Advanced Bitwise Tricks Quiz Quiz

Sharpen your knowledge of advanced bitwise operations, bit hacks, and practical binary manipulation techniques with these thoughtfully crafted questions. Improve your understanding of efficient algorithms and problem-solving strategies that leverage bitwise tricks.

  1. Detecting Odd Numbers Efficiently

    Which bitwise operation can be used to determine if an integer is odd by examining its least significant bit?

    1. num ^ 0
    2. num u003Cu003C 1
    3. num u0026 1
    4. num | 2

    Explanation: The expression 'num u0026 1' checks the least significant bit, which will be 1 if the number is odd and 0 if the number is even. The '| 2' operation would not directly test parity and may alter the number. '^ 0' leaves the number unchanged, serving no purpose in this context. 'u003Cu003C 1' shifts bits leftward, effectively multiplying by 2, and does not provide parity information.

  2. Clearing a Specific Bit

    Given an integer 'num' and a bit position 'k', which expression clears (sets to zero) the k-th bit using bitwise manipulation?

    1. num ^ (1 u003Eu003E k)
    2. num | (1 u003Cu003C k)
    3. num u0026 (1 u003Eu003E k)
    4. num u0026 ~(1 u003Cu003C k)

    Explanation: To clear a specific bit, create a mask with '1 u003Cu003C k', invert it with the NOT operator '~', and AND it with the original number. '| (1 u003Cu003C k)' sets the bit rather than clearing it. '^ (1 u003Eu003E k)' and 'u0026 (1 u003Eu003E k)' reference a right shift, which is not appropriate for this operation and does not properly target the k-th bit.

  3. Counting the Number of Set Bits

    Which bitwise trick is commonly used to count the number of 1-bits in an integer efficiently?

    1. Repeatedly applying n = n u0026 (n - 1)
    2. Dividing n by 2 repeatedly
    3. Multiplying n by 2 in a loop
    4. Toggling each bit with ~n

    Explanation: Applying 'n = n u0026 (n - 1)' clears the lowest set bit each time and is a well-known trick for efficiently counting set bits in an integer. Multiplying by 2 and dividing by 2 shift the bit positions but do not directly count set bits. Toggling each bit with NOT does not help count the number of 1-bits in the original number.

  4. Swapping Two Values with Bitwise XOR

    How can you swap two integer variables 'a' and 'b' without using a temporary variable, utilizing bitwise operations?

    1. a = a u003Cu003C b; b = a u003Eu003E a; a = a u003Cu003C b
    2. a = a u0026 b; b = a | b; a = a u0026 b
    3. a = a ^ b; b = a ^ b; a = a ^ b
    4. a = a | b; b = a | b; a = a u0026 b

    Explanation: The XOR swap algorithm allows two integers to be swapped without a temporary variable using three XOR operations. The AND and OR variant given would not swap values and may lose information. The shift-based option moves bits but does not preserve the original values. The repeated use of OR and AND in the last option does not accomplish a correct swap.

  5. Isolating the Rightmost Set Bit

    What expression isolates the rightmost set bit (least significant 1) in a positive integer 'x'?

    1. x u003Cu003C 1
    2. x | (x - 1)
    3. x ^ (x - 1)
    4. x u0026 -x

    Explanation: The operation 'x u0026 -x' isolates the rightmost set bit efficiently by leveraging two's complement representation. The expression '| (x - 1)' turns on lower bits instead of isolating a single bit. The XOR with (x - 1) flips lower bits but does not isolate the rightmost one. Left-shifting moves bits to the left, not isolating any particular set bit.