Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which statement is suitable to check 3rd (count from 0) bit is high (set) or not?

  • A.(num & (1<<3))
  • B.(num & 0x08)
  • C.(num & 0x03)
  • D.
    Both (1) and (2)

Answer: D

Correct answer: Both (1) and (2)

The two expressions build the same mask by different routes. 1<<3 shifts a single 1 into position 3, giving \( 1000_2 \); 0x08 is that same value written directly. ANDing with either isolates bit 3.

The third option is the trap: 0x03 is \( 0011_2 \), which selects bits 0 and 1 - a mask for two low bits, not for bit 3. The shift form is generally preferred in code because the bit number appears literally, so 1<<k reads as "bit k" without the reader converting hexadecimal in their head.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.