Question 71
HardHow do you turn off the rightmost set bit of n?
- A.
n | (n + 1)
- B.
~n
- C.
n & (n − 1)
- D.
n ^ (n + 1)
Sign up free to see the answer and a step-by-step explanation.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
How do you turn off the rightmost set bit of n?
n | (n + 1)
~n
n & (n − 1)
n ^ (n + 1)
Sign up free to see the answer and a step-by-step explanation.
How do you set all bits below and including the lowest set bit? (for n = 12, giving 15)
n | (n − 1)
n >> 1
n ^ n
n & (n − 1)
Sign up free to see the answer and a step-by-step explanation.
What does popcount(a ^ b) compute?
The Hamming distance between a and b
a + b
The larger of a and b
a × b
Sign up free to see the answer and a step-by-step explanation.
How many subsets does a bitmask enumeration over 20 items visit?
400
20
2²⁰ ≈ 1 million
20!
Sign up free to see the answer and a step-by-step explanation.
How do you iterate over all submasks of a mask m efficiently?
s = m; while s: process(s); s = (s − 1) & m
s = m >> 1 repeatedly
s = m ^ 1 repeatedly
for s in range(m)
Sign up free to see the answer and a step-by-step explanation.
What is the XOR of all numbers from 1 to n when n % 4 == 3?
n
0
1
n + 1
Sign up free to see the answer and a step-by-step explanation.
Why can left-shifting a signed int in C be undefined behaviour?
Only right shifts are defined
Because of endianness
Shifting a 1 into or past the sign bit (or shifting negatives) overflows
Shifts are always undefined
Sign up free to see the answer and a step-by-step explanation.
Which bit trick checks whether integers a and b have opposite signs?
(a & b) < 0
(a | b) > 0
a + b == 0
(a ^ b) < 0
Sign up free to see the answer and a step-by-step explanation.
How do you swap the nibbles (4-bit halves) of a byte x?
x >> 4
x ^ 0xFF
x << 4
((x & 0x0F) << 4) | ((x & 0xF0) >> 4)
Sign up free to see the answer and a step-by-step explanation.
What is the result of 0b1011 & 0b0110, 0b1011 | 0b0110 and 0b1011 ^ 0b0110?
2, 15, 9
2, 15, 13
6, 15, 13
2, 11, 13
Sign up free to see the answer and a step-by-step explanation.