CompTIA Tech+ (FC0-U71)IT Concepts and TerminologyHard
A programmer is writing a low-level routine that needs to directly manipulate specific bits within a byte. If they need to set the 3rd bit (0-indexed) of a byte to 1, while leaving all other bits unchanged, which binary operation should they use?
- AOR
- BAND
- CNOT
- DXOR
Show answer & explanationAnswer & explanation
Correct answer: A. OR
To set a specific bit to 1, regardless of its current state, the OR operation is used with a mask where only that specific bit is 1. For example, to set the 3rd bit (index 2), the mask would be 00000100 (binary) or 0x04 (hex). Any bit ORed with 0 remains unchanged, and any bit ORed with 1 becomes 1.
Why the other options are wrong
- B. The AND operation is used to clear (set to 0) specific bits or to check if a bit is set.
- C. The NOT operation inverts all bits in a byte; it does not selectively set a single bit while preserving others.
- D. The XOR operation is used to toggle (flip) specific bits.
Bitwise OR Operation
A binary operation that compares corresponding bits of two numbers. If either bit is 1, the resulting bit is 1. Otherwise, it is 0.
- Used to set specific bits to 1 without affecting other bits.
- Syntax often `A | B` in programming languages.
- Common in low-level programming, flag manipulation, and device control.
Memory trick: AND, OR, XOR: Logic's Core!