Bitwise Operations and C++ Fundamentals Quiz Quiz

Assess your understanding of bit manipulation techniques and key C++ programming concepts with a focus on binary operations, data types, and logical reasoning. Enhance your C++ skills by exploring bitwise operators, memory representation, and common pitfalls in beginner-level applications.

  1. Bitwise AND Example

    What is the result of the expression 6 u0026 3 in C++?

    1. 2
    2. 5
    3. 0
    4. 9

    Explanation: The bitwise AND operator u0026 compares each bit of its operands; 6 is 110 in binary and 3 is 011. Their AND result is 010, which is 2 in decimal. Option 0 is the result of AND-ing two numbers with no common set bits, which is not the case here. Option 5 and 9 do not represent the correct binary operation output.

  2. Bitwise OR Operator Function

    Which value results from 5 | 2 in C++?

    1. 2
    2. 7
    3. 3
    4. 1

    Explanation: The bitwise OR | sets a bit to 1 if either operand has a 1 in that position. 5 in binary is 101 and 2 is 010; OR-ing them gives 111, which is 7 decimal. Choosing 2 or 1 ignores the operation, giving only one operand's value. Option 3 is incorrect as it miscalculates the bitwise effect.

  3. Bitwise XOR Outcome

    What is the output of 8 ^ 5 in C++?

    1. 5
    2. 13
    3. 10
    4. 3

    Explanation: The bitwise XOR ^ operator compares bits, resulting in 1 when bits differ. 8 is 1000, 5 is 0101; XOR gives 1101, which is 13 in decimal. Answering 10 confuses addition with bitwise XOR. 3 and 5 are incorrect since their binary results don't match the calculation.

  4. Bitwise Complement Application

    If x is an int with value 0, what is the result of ~x in C++?

    1. 4294967295
    2. 1
    3. 0
    4. -1

    Explanation: The bitwise NOT ~ inverts all bits. On a typical system, ~0 results in all bits set, which is -1 for signed integers due to two’s complement. Option 1 represents a single set bit, not all. Zero is unchanged only if ~0 means no inversion, which is incorrect. 4294967295 is for unsigned, but int defaults to signed.

  5. Left Shift Operator

    What does the expression (3 u003Cu003C 2) evaluate to in C++?

    1. 12
    2. 8
    3. 6
    4. 4

    Explanation: Shifting left by 2 multiplies the number by 4. 3 u003Cu003C 2 means 3 times 4, which is 12. Option 6 is from shifting once, not twice. Option 8 is the result of 2 u003Cu003C 2, not 3. Option 4 happens only when shifting 1 u003Cu003C 2.

  6. Binary Literal Representation

    Which of the following represents the binary literal for decimal 10 in C++?

    1. 10b
    2. 0xA
    3. b1010
    4. 0b1010

    Explanation: In C++, binary literals start with 0b or 0B, so 0b1010 is correct for 10. 0xA is hexadecimal, not binary. 'b1010' and '10b' are not valid C++ literal formats.

  7. Swap Using XOR

    Which property of XOR allows swapping two variables without a temporary variable?

    1. A ^ 0 = A
    2. A u0026 B = B u0026 A
    3. A | B = B | A
    4. A ^ B ^ B = A

    Explanation: The property A ^ B ^ B = A is fundamental for XOR swaps, as XOR-ing twice with the same value restores the original. The AND and OR properties are symmetric but don’t enable this swap. 'A ^ 0 = A' is true but not relevant to the 3-step XOR swap trick.

  8. C++ Data Types and Bit Sizes

    What is the minimum number of bits in a standard C++ 'char' type?

    1. 8
    2. 4
    3. 16
    4. 32

    Explanation: C++ guarantees at least 8 bits for a 'char' type, making 8 the correct answer. 16 or 32 bits are common for 'int' or 'long' types on some systems. 4 bits is too small and not standard for 'char'.

  9. Bitwise Operator Precedence

    In C++, which operator has higher precedence: bitwise AND (u0026) or bitwise XOR (^) ?

    1. ^
    2. |
    3. They have the same
    4. u0026

    Explanation: Bitwise AND (u0026) has higher precedence than XOR (^), so it is evaluated first in expressions. If they had equal precedence, their order would be ambiguous. Option | is unrelated to this comparison.

  10. Right Shift and Sign Bit

    What is the result of evaluating -8 u003Eu003E 1 on a system using two’s complement representation in C++?

    1. 4
    2. -8
    3. -4
    4. 0

    Explanation: For negative numbers, right shift preserves the sign bit, meaning -8 (in two’s complement) shifted right by one yields -4. Option -8 means no shift occurs; option 4 drops the negative sign incorrectly. Zero is the result for right-shifting zero, not -8.