|
1 | 1 | --- |
2 | 2 | title: Linked Lists and Bitwise Operators |
3 | | -date: |
| 3 | +date: 2025-09-25/30 |
4 | 4 | --- |
5 | 5 |
|
| 6 | +## Bitwise operators in C |
| 7 | + |
| 8 | +Integer values are stored as bits. Bitwise operators operate on each bit position. |
| 9 | + |
| 10 | +Assume 8-bit examples for clarity. |
| 11 | + |
| 12 | +### AND `&` |
| 13 | + |
| 14 | +- Result bit is 1 only if both input bits are 1. |
| 15 | + |
| 16 | +Example: |
| 17 | + |
| 18 | +- $00000111_2 \quad \\& \quad 00000100_2 = 00000100_2$ |
| 19 | + |
| 20 | +Truth table: |
| 21 | + |
| 22 | +- $0 \\& 0 = 0$ |
| 23 | +- $0 \\& 1 = 0$ |
| 24 | +- $1 \\& 0 = 0$ |
| 25 | +- $1 \\& 1 = 1$ |
| 26 | + |
| 27 | +Typical uses: |
| 28 | + |
| 29 | +- Masking to zero out some bits. |
| 30 | +- Testing whether specific bits are set. |
| 31 | + |
| 32 | +### OR `|` |
| 33 | + |
| 34 | +- Result bit is 1 if at least one input bit is 1. |
| 35 | + |
| 36 | +Example: |
| 37 | + |
| 38 | +- $00000111_2 \mid 00000100_2 = 00000111_2$ |
| 39 | + |
| 40 | +### XOR `^` |
| 41 | + |
| 42 | +- Result bit is 1 if the input bits are different. |
| 43 | + |
| 44 | +Truth table: |
| 45 | + |
| 46 | +- $0 \oplus 0 = 0$ |
| 47 | +- $0 \oplus 1 = 1$ |
| 48 | +- $1 \oplus 0 = 1$ |
| 49 | +- $1 \oplus 1 = 0$ |
| 50 | + |
| 51 | +> Using $\oplus$ here means XOR. |
| 52 | +
|
| 53 | +Typical uses: |
| 54 | + |
| 55 | +- Flipping bits under a mask. |
| 56 | +- Simple parity or checksum calculations. |
| 57 | +- Toy "encryption" where the same key applied twice recovers the original. |
| 58 | + |
| 59 | +### NOT `~` |
| 60 | + |
| 61 | +- Flips every bit (0 becomes 1 and 1 becomes 0). |
| 62 | + |
| 63 | +If we consider 8 bits, then |
| 64 | + |
| 65 | +- $\sim 00000101_2 = 11111010_2.$ |
| 66 | + |
| 67 | +In two's complement, this is closely related to negation: $-x = \sim x + 1$. |
| 68 | + |
| 69 | +### Shift operators `<<` and `>>` |
| 70 | + |
| 71 | +- `x << k` shifts bits of `x` left by $k$ positions (fills with zeros on the right). |
| 72 | +- `x >> k` shifts bits right. For unsigned values this fills with zeros on the left; for signed values it may copy the sign bit. |
| 73 | + |
| 74 | +Shifting left by $k$ is equivalent to multiplying by $2^k$ when there is no overflow. |
| 75 | + |
| 76 | +Example: |
| 77 | + |
| 78 | +- $00000101_2 << 1 = 00001010_2$ which is $5$ shifted to $10$. |
| 79 | + |
6 | 80 | ## Linked lists in C |
7 | 81 |
|
8 | 82 | A linked list is a dynamic data structure built from nodes that point to one another. |
@@ -98,77 +172,3 @@ void print_list(CELL *head) { |
98 | 172 | - Overwriting `head` while traversing, then losing access to the list. |
99 | 173 | - Forgetting to initialize `next` for new nodes. |
100 | 174 | - Failing to `free` nodes eventually, causing memory leaks. |
101 | | -
|
102 | | -## Bitwise operators in C |
103 | | -
|
104 | | -Integer values are stored as bits. Bitwise operators operate on each bit position. |
105 | | -
|
106 | | -Assume 8-bit examples for clarity. |
107 | | -
|
108 | | -### AND `&` |
109 | | -
|
110 | | -- Result bit is 1 only if both input bits are 1. |
111 | | -
|
112 | | -Example: |
113 | | -
|
114 | | -- $00000111_2 \quad \\& \quad 00000100_2 = 00000100_2$ |
115 | | -
|
116 | | -Truth table: |
117 | | -
|
118 | | -- $0 \\& 0 = 0$ |
119 | | -- $0 \\& 1 = 0$ |
120 | | -- $1 \\& 0 = 0$ |
121 | | -- $1 \\& 1 = 1$ |
122 | | -
|
123 | | -Typical uses: |
124 | | -
|
125 | | -- Masking to zero out some bits. |
126 | | -- Testing whether specific bits are set. |
127 | | -
|
128 | | -### OR `|` |
129 | | -
|
130 | | -- Result bit is 1 if at least one input bit is 1. |
131 | | -
|
132 | | -Example: |
133 | | -
|
134 | | -- $00000111_2 \mid 00000100_2 = 00000111_2$ |
135 | | -
|
136 | | -### XOR `^` |
137 | | -
|
138 | | -- Result bit is 1 if the input bits are different. |
139 | | -
|
140 | | -Truth table: |
141 | | -
|
142 | | -- $0 \oplus 0 = 0$ |
143 | | -- $0 \oplus 1 = 1$ |
144 | | -- $1 \oplus 0 = 1$ |
145 | | -- $1 \oplus 1 = 0$ |
146 | | -
|
147 | | -> Using $\oplus$ here means XOR. |
148 | | -
|
149 | | -Typical uses: |
150 | | -
|
151 | | -- Flipping bits under a mask. |
152 | | -- Simple parity or checksum calculations. |
153 | | -- Toy "encryption" where the same key applied twice recovers the original. |
154 | | -
|
155 | | -### NOT `~` |
156 | | -
|
157 | | -- Flips every bit (0 becomes 1 and 1 becomes 0). |
158 | | -
|
159 | | -If we consider 8 bits, then |
160 | | -
|
161 | | -- $\sim 00000101_2 = 11111010_2.$ |
162 | | -
|
163 | | -In two's complement, this is closely related to negation: $-x = \sim x + 1$. |
164 | | -
|
165 | | -### Shift operators `<<` and `>>` |
166 | | -
|
167 | | -- `x << k` shifts bits of `x` left by $k$ positions (fills with zeros on the right). |
168 | | -- `x >> k` shifts bits right. For unsigned values this fills with zeros on the left; for signed values it may copy the sign bit. |
169 | | -
|
170 | | -Shifting left by $k$ is equivalent to multiplying by $2^k$ when there is no overflow. |
171 | | -
|
172 | | -Example: |
173 | | -
|
174 | | -- $00000101_2 << 1 = 00001010_2$ which is $5$ shifted to $10$. |
0 commit comments