Exercises
Explore how programs manipulate individual bits using AND, OR, XOR, NOT, shifts, and masks. This quiz covers binary calculations, setting and clearing bits, testing flags, extracting bit fields, unsigned overflow, parity, and common bit manipulation techniques. Questions progress from basic operator behavior to practical expressions used in systems programming, compact data storage, permissions, and hardware-oriented code.
Answer the questions below and check the explanation for each answer.
0/20 answered
Auto audio on: the next questions will be read aloud when you click Continue.
A bit has exactly two possible states: 0 and 1. Multiple bits can be combined to represent larger values.
AND produces 1 only when both corresponding bits are 1. Comparing 1100 with 1010 gives 1000.
OR produces 1 when at least one corresponding bit is 1. Applying it to 1100 and 1010 produces 1110.
XOR produces 1 when corresponding bits are different. The unequal positions in 1100 and 1010 produce 0110.
Bitwise NOT flips every bit. In an 8-bit value, the four leading zeros become ones and the four trailing ones become zeros, giving 11110000.
Left-shifting an unsigned value by two positions multiplies it by 2² when no significant bits are discarded. Therefore, 5 × 4 = 20.
An unsigned right shift by three positions divides by 2³ and discards any remainder. Since 40 ÷ 8 = 5, the result is 5.
OR sets a result bit to 1 wherever the mask contains 1. Where the mask contains 0, the corresponding bit from x remains unchanged.
Inverting the mask places zeros at the selected positions. AND with that inverted mask clears those positions while preserving positions where the inverted mask has ones.
XOR with 1 flips a bit, while XOR with 0 leaves it unchanged. Therefore, x ^ mask toggles exactly the selected bits.
AND keeps only bits selected by the mask. A nonzero result means that at least one selected bit was already set in x.
The expression first clears bits 2–4 of x. It then limits v to three bits, shifts those bits into positions 2–4, and combines them with OR.
Independent flags are combined with OR. READ is 1 and EXECUTE is 4, so their combined value is 1 | 4 = 5.
Shifting 1 left by five positions produces 2⁵. Therefore, the mask has decimal value 32.
The expression x & -x isolates the least significant set bit of a two's-complement integer. Binary 40 is 101000, whose lowest set bit has value 8.
Subtracting 1 changes the lowest set bit to 0 and changes lower zeros to ones. AND with the original value therefore removes only that lowest set bit.
The sequence contains five 1 bits. Because five is odd, XORing all bits produces an odd-parity result of 1.
The mask 0x0F has ones in its lowest four positions. AND preserves those four bits and clears all higher bits.
Right-shifting by three moves original bits 3–5 into positions 0–2. AND with 0b111 then removes all bits outside the extracted field.
A left rotation moves every bit left and wraps the former most significant bit into the least significant position. Rotating 10000001 produces 00000011.
Video Courses
Ideal for beginners
Thousands of online courses in video, ebooks and audiobooks.
To test your knowledge during online courses
Generated directly from your cell phone's photo gallery and sent to your email
Download our app via QR Code or the links below:.
+ 10 million
students
Free and Valid
Certificate
60 thousand free
exercises
4.8/5 rating in
app stores
Free courses in
video and ebooks