Skip to content

Commit 7039d13

Browse files
Added notes for Bit Manipulation
1 parent 849a15c commit 7039d13

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

BitManipulation.md

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
## Bit Manipulation:
2+
N = 6163<sub>10</sub> = (6×10<sup>3</sup>) + (1×10<sup>2</sup>) + (6×10<sup>1</sup>) + (3×10<sup>0</sup>)<br>
3+
In base 2 (binary) instead of 10's power we represent in 2's power. To convert keep dividing number by two and append remainder to a string. In the end reverse the string.
4+
5+
AND = A.B<br>
6+
OR = A + B<br>
7+
NOT = 1 - A = !A<br>
8+
XOR = A!B + !AB<br>
9+
10+
Moore's Law: <br>
11+
!(A + B) = A!.B!<br>
12+
!(A.B) = A! + B!
13+
14+
XNOR = !XOR = (A + B!).(A! + B)<br>
15+
NOR = !OR = A!.B!<br>
16+
NAND = !AND = A! + B!<br>
17+
18+
> If XOR of two numbers/string/any data is 0 then both are same.
19+
20+
If between two number there's a bit difference of exactly one by XOR then it is called Gray Code. It is used in signal detection to check if there's any wrong signal.
21+
<center>
22+
23+
| Type | Size (bit) | Range |
24+
| ------------- |:-------------|:-----------------------------------|
25+
| Bit | 1 | 0 or 1 |
26+
| Nibble | 4 | 0 to 2<sup>4</sup>-1 |
27+
| Byte | 8 | 0 to 2<sup>8</sup>-1 |
28+
| Word | 16 | 0 to 2<sup>16</sup>-1 |
29+
| Double | 32 | 0 to 2<sup>32</sup>-1 |
30+
| Qword | 64 | 0 to 2<sup>64</sup>-1 |
31+
32+
</center>
33+
34+
Gb (Gigabits), GB (Gigabytes), Gib (10<sup>3</sup> bits), GiB (10<sup>3</sup> bytes)
35+
36+
Hexadecimals: **0x<span style="color:blue">AA</span><span style="color:red">BB</span><span style="color:green">CC</span><span style="color:orange">DD</span>**<br>
37+
Different colors represent a byte (AA 1 byte). Nibble has 4 bits i.e. 2<sup>4</sup> so a hexadecimal value which is from 0-15 (10 - A, B, C, D, E, F)
38+
39+
Least Significant Bit (LSB) & Most Significant Bit (MSB): 99 in binary (MSB part)01100011(LSB part) so in MSB 01100011 in LSB 11000110<br>
40+
Endiness (Storing data in memory) : Little Endian (LSB), Big Endian (MSB)
41+
>We use LSB
42+
43+
Finding i<sup>th</sup> bit:<br>
44+
```c++
45+
X(1<<4) >> 4
46+
output:
47+
010x00
48+
00010x
49+
000x00
50+
x
51+
```
52+
53+
1's Complement: Toggling every bit ~<br>
54+
2's Complement:<br>
55+
-X = !X + 1<br>
56+
X = !(-X-1)
57+
58+
<center>
59+
60+
| Decimal | Binary | Hexadecimal | Decimal | Binary | Hexadecimal |
61+
| ------------- |:-------------|:-------------| :------------ |:-------------|:-------------|
62+
| 0 | 0000 | 0x0 | 0 | 0000 | 0x9 |
63+
| 1 | 0001 | 0x1 | -1 | 1111 | 0xA |
64+
| 2 | 0010 | 0x2 | -2 | 1110 | 0xB |
65+
| 3 | 0011 | 0x3 | -3 | 1101 | 0xC |
66+
| 4 | 0100 | 0x4 | -4 | 1011 | 0xD |
67+
| 5 | 0101 | 0x5 | -5 | 1011 | 0xE |
68+
| 6 | 0110 | 0x6 | -6 | 1010 | 0xF |
69+
| 7 | 0111 | 0x7 | -7 | 1001 | 0x7 |
70+
| 8 | 1000 | 0x8 | -8 | 1000 | 0x8 |
71+
72+
1 in 8th first value means it's a negative number which is not true so range is -8 to 7 only
73+
</center>
74+
75+
```c++
76+
0000111110110011
77+
// << 3 yields:
78+
0111110110011000
79+
// >> 3 yields:
80+
0000000111110110
81+
82+
// Multiplication
83+
i * 8; // normal
84+
i << 3; // bitwise [8 = 2^3, so use 3]
85+
86+
// Division
87+
i / 16; // normal
88+
i >> 4; // bitwise [16 = 2^4, so use 4]
89+
90+
// Modulus
91+
i % 4; // normal
92+
i & 3; // bitwise [4 = 1 << 2, apply ((1 << 2) - 1), so use 3]
93+
i % 2^i = n & (2^i - 1)
94+
95+
Bitwise shifts << >> shouldn't be used on negative numbers.
96+
```
97+
```c
98+
/*
99+
Booth's Multiplication Algo: (01011 x 01011)
100+
01011 x 1 = 01011
101+
010110 x 1 = 010110
102+
0101100 x 0 = 0000000
103+
01011000 x 1 = 01011000
104+
1111001
105+
*/
106+
107+
int mul(int a, int b)
108+
{
109+
int ans = 0;
110+
for (int i = 0 ; i < 32; i++)
111+
{
112+
if (b & 1) ans += a;
113+
b = b>>1;
114+
a = a<<1;
115+
}
116+
return ans;
117+
}
118+
```

0 commit comments

Comments
 (0)