What is a bitwise AND operation?
A bitwise AND operation compares each bit of two numbers and returns a new number where each bit is set to 1 only if both corresponding bits of the operands are 1.
How does the OR operation work in binary?
The OR operation sets each bit to 1 if at least one of the corresponding bits of the operands is 1.
Can you explain XOR with an example?
XOR (exclusive OR) returns a 1 for each bit position where the corresponding bits of either but not both operands are 1. For example, 5 XOR 3 results in 6.
What is the purpose of NOT in bitwise operations?
NOT inverts all the bits of a number; it changes 0s to 1s and 1s to 0s.
How do left and right shifts work?
Left shift moves all bits to the left by a specified number of positions, filling vacated bits with zeros. Right shift moves bits to the right, discarding bits shifted off and often padding with the sign bit (for signed numbers).
When would you use bitwise operations in programming?
Bitwise operations are used for optimizing performance, manipulating data at a low level, and implementing algorithms that require direct binary manipulation.
Can you give an example of using bitwise NOT in a real-world scenario?
In programming, bitwise NOT can be used to toggle bits or invert conditions. For instance, it’s often used in flags management to enable or disable certain features by flipping specific bits.