-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbitsNbytesManup.cpp
More file actions
122 lines (104 loc) · 2.91 KB
/
bitsNbytesManup.cpp
File metadata and controls
122 lines (104 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* File: bitsNbytesManup.c
* Author: Karim El-Rayes
* Created on August 19, 2016
* Disclaimer: this code is available for public use
* use it on your own responsibility and liability,
* The developer doesn't claim any liability for this code.
* You can distribute and share the code with anyone without
* any prior permissions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitsNbytesManup.h"
//Two bytes concatenation function
//unsigned int ByteCat2(unsigned char lowByte, unsigned char highByte, unsigned int PlacestoShift)
unsigned int ByteCat2(unsigned char lowByte, unsigned char highByte)
{
unsigned int MSB = 0;
unsigned int LSB = 0;
unsigned int temp = 0;
unsigned int value = 0;
MSB = highByte;
LSB = lowByte;
temp = MSB << 8; //<< PlacestoShift;
value = temp | LSB;
return value;
}
//Three bytes concatenation function
unsigned int ByteCat3(unsigned char lowByte, unsigned char middleByte, unsigned char highByte)
{
unsigned int HiByte = 0;
unsigned int MidByte = 0;
unsigned int LoByte = 0;
unsigned int temp = 0;
unsigned int value = 0;
HiByte = highByte;
MidByte = middleByte;
LoByte = lowByte;
temp = HiByte << 8;
temp = (temp | MidByte) << 8;
value = temp | LoByte;
return value;
}
//Four bytes concatenation function
unsigned int ByteCat4(unsigned char Byte0, unsigned char Byte1, unsigned char Byte2, unsigned char Byte3)
{
unsigned int byte0 = 0;
unsigned int byte1 = 0;
unsigned int byte2 = 0;
unsigned int byte3 = 0;
unsigned int temp = 0;
unsigned int value = 0;
byte0 = Byte0;
byte1 = Byte1;
byte2 = Byte2;
byte3 = Byte3;
temp = byte3 << 8;
temp = (temp | byte2) << 8;
temp = (temp | byte1) << 8;
value = temp | byte0;
return value;
}
//Check if bit is 0 or 1
bool CheckBit(unsigned int number, unsigned int position)
{
unsigned int temp = 0;
unsigned int comp = 0x01;
bool value = false;
temp = number >> position;
if(temp & comp)
value = true;
else
value = false;
return value;
}
//Sets a specific bit to 1
unsigned int SetBit(unsigned int number, unsigned int position)
{
unsigned int temp = 0x01;
unsigned int value = 0;
temp = temp << position;
value = temp | number;
return value;
}
//Resets a specific bit to 0
unsigned int ResetBit(unsigned int number, unsigned int position)
{
unsigned int temp = 0x01;
unsigned int value = 0;
temp = ~(temp << position);
//temp = temp & 0xff;
value = temp & number;
return value;
}
//Get the Two's complement of a number
unsigned int TwoComplement(unsigned int number, unsigned int SizeInBits)
{
unsigned int value = 0;
value = ~number;
value = (value + 0x01) << 32-SizeInBits; //shift right to remove the extra 1's, SizeInBits can be any value between 0 and 32
value = value >> 32-SizeInBits; //shift left to return the number to LSB position
return value;
}