-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources
150 lines (120 loc) · 4.55 KB
/
resources
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
- Bitwise:
Lets assume int a = 0x00FF00FF
<< x : shifts the bit by x to the left. Essentially multiplies the number by 2^x
>> x : shifts the bit by x to the right. Essentially divides the number by 2^x
When we want to shift a byte to the left or to the right is use x=8
uint16_t num = 0x00FF;
num = num << 8; /* num now will be 0xFF00; the reverse applies for the >> operator
- masks:
uint8_t mask0 = 0b0000'0001 /* or 1 << 0 */
uint8_t mask1 = 0b0000'0010 /* or 1 << 1 */
uint8_t mask2 = 0b0000'0100 /* or 1 << 2 */
uint8_t mask3 = 0b0000'1000 /* or 1 << 3 */
uint8_t mask4 = 0b0001'0000 /* or 1 << 4 */
uint8_t mask5 = 0b0010'0000 /* or 1 << 5 */
uint8_t mask6 = 0b0100'0000 /* or 1 << 6 */
uint8_t mask7 = 0b1000'0000 /* or 1 << 7 */
- Testing a bit
uint8_t num = 0b1111'0101
uint8_t mask = 0b0000'0100;
if (num & mask)
/* bit is set. AND will produce
* 0b0000'0100 which is true because it is not 0 so the mask is applied
*/
- Setting a bit
uint8_t num = 0b0000'0101
uint8_t mask = 0b0001'0000
num |= mask // will produce 0b0001'0101 which is the same as the previous one with the mask bit set
- Resetting a bit
uint8_t num = 0b0000'0101
uint8_t mask = 0b0000'0100
num &= ~mask // will produce 0b0001'0101 which is the same as the previous one with the mask bit set
/* NOT will create the inverse of the mask -> 0b1111'1011
* AND sets to 1 only if both have the bit 1 but the mask set the bit to its opposite
* so if it is set it will be reset but if it is not set it will not be as the ~mask will set
* bit to 0 and the AND doesn't set to 1 if both are 0.
*/
/* We can reset multiple bits at once by OR ing the masks together */
num &= (mask0 | mask2 | mask3 )
- Toggling a bit on and off
/* we use the bitwise XOR */
/* XOR sets the respective bit to 1 if and only if the inputs differ (one is true, one is false) */
uint8_t num = 0b0000'0101
uint8_t mask = 0b0000'0100
num ^= mask /* creates 0b0000'0001
num ^= mask /* again creates 0b0000'0101
- creating a custom RGB int
/* ints have 4 bytes. This is how we normally symbolize colours for our screens
* ---------------------------------
* | Alpha | Red | Green | Blue |
* ---------------------------------
* | 8 bits | 8 bits | 8bits | 8bits |
* ---------------------------------
* we can create an in by shifting the values by the respective bits and OR ing them.
*/
uint8_t alpha = 0x01
uint8_t red = 0xFF
uint8_t green = 0x7c
uint8_t blue = 0x6a
int color = (alpha << 24) | (red << 16) | (green << 8) | blue
/* result will be 0x01ff7c6a */
- Multiplication of x by y using bitwise operations:
/*
* lets suppose that both numbers are positive
* for every bit significant bit of y while y is > 0 we shift x to the left multiplying it by 2
*/
int result;
while (y) {
if ((y & 1) != 0)
result += x;
x = x << 1;
y = y >> 1;
}
- Reversing the bits in a 8bit int;
uint8_t temp;
uint8_t num = 0b10110101;
for (int i=0; i < 8; i++) {
temp = (temp << 1) | (y & 1) /* shift the bit to the left and save the most significant bit */
num = num >> 1;
}
- Swapping a 8bit int
uint8_t num = 0b10110101; /* equivalent to 0xb5
num = (num >> 4) | (num << 4) /* equivalent to 0x5b
- Addition in binary
/* since we are little endian we go from left to right and use the following board for the carry bit
* ---------------
* | x | y | x + y |
* ---------------
* | 0 | 0 | 0 |
* | 1 | 0 | 1 |
* | 0 | 1 | 1 |
* | 1 | 1 | 0 | // Carry over
* --------------- // 1 + 1 with a carry is 1 and the carry remains.
// 1 + 0 with carry is 0 and the carry remains
ex 0 0 0 1 0 0 0 1
+ 0 1 0 1 0 1 1 1
---------------
0 1 1 0 1 1 1 0
- Subtraction in binary
/* can be represented as Addition of the negative number
* we can calculate the negative equivalent using with two's compliment
- One's Compliment
/* flipping the zeros to ones and vice versa(NOT)
* comes because adding the two numbers will always give
* a perfect all ones
- Twos's Compliment
/* flipping the zeros to ones and vice versa(NOT)
* adding 1 and ignoring all overflows.
- Sign extension
Need to represent a value given in certain bits by using a larger amount of bits
/* if the sign of the number is 0(positive) then we fill the extra bits with 0 */
uint8_t num = 0x04
num = (num << 24) >> 24 // makes it a 32 bit number looking like this 0x00 00 00 04
/* Negative case
* number 4 in binary is 0b0100
* to convert to negative we get 2's compliment
* NOT original value and then add one to it
* 0b1011
+ 0b0001
---------
0b1100