Important Introduction to Programming Short Questions
Chapter: 21
Question 1:
What happens when you XOR a number with itself?
Answer:
The result is 0. XORing a number with itself cancels out all the bits.
Chapter: 21
Question 2:
What does the NOT operator (~) do to a binary number?
Answer:
It inverts all the bits of the number, changing 1s to 0s and 0s to 1s.
Chapter: 21
Question 3:
What is the result of applying the NOT operator to the number 8?
Answer:
The result is -9 in a signed integer context, as the NOT operator flips all the bits of 8 (00001000) to (11110111).
Chapter: 21
Question 4:
Describe the left shift operator (<<).
Answer:
The left shift operator shifts all bits in a binary number to the left by a specified number of positions, effectively multiplying the number by 2 for each shift.
Chapter: 21
Question 5:
What is the effect of right shifting a number using the >> operator?
Answer:
Right shifting a number divides it by 2 for each shift, discarding the least significant bits.
Chapter: 21
Question 6:
What does the term "bit manipulation" refer to?
Answer:
Bit manipulation involves directly working with bits to perform operations like setting, clearing, or toggling specific bits.
Chapter: 21
Question 7:
How can you use bitwise operators to check if a specific bit is set?
Answer:
Use bitwise AND with a mask that has only the bit of interest set to 1. If the result is non-zero, the bit is set.
Chapter: 21
Question 8:
What is a bit field, and where is it commonly used?
Answer:
A bit field is a data structure that allows the efficient storage of multiple boolean flags in a single byte or word. It is commonly used in operating systems and file attributes.
Chapter: 21
Question 9:
What is the significance of the most significant bit (MSB) in unsigned integers?
Answer:
In unsigned integers, the MSB is used to represent the highest value bit, and it helps in determining the size and range of the integer value.
Chapter: 21
Question 10:
How can you swap two numbers using XOR without a temporary variable?
Answer:
Use the following steps: a = a ^ b; b = b ^ a; a = a ^ b. This will swap the values of a and b.