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.
What is the result of the expression 6 u0026 3 in C++?
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.
Which value results from 5 | 2 in C++?
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.
What is the output of 8 ^ 5 in C++?
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.
If x is an int with value 0, what is the result of ~x in C++?
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.
What does the expression (3 u003Cu003C 2) evaluate to in C++?
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.
Which of the following represents the binary literal for decimal 10 in C++?
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.
Which property of XOR allows swapping two variables without a temporary variable?
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.
What is the minimum number of bits in a standard C++ 'char' type?
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'.
In C++, which operator has higher precedence: bitwise AND (u0026) or bitwise XOR (^) ?
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.
What is the result of evaluating -8 u003Eu003E 1 on a system using two’s complement representation in C++?
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.