Bitwise Manipulation: Introduction
Let’s go over the Bitwise Manipulation pattern, its real-world applications, and some problems we can solve with it.
We'll cover the following
About the pattern
In programming, everything is stored in the computer’s memory as sequences of 0s and 1s, which are called bits. Bitwise manipulation is the process of modifying bits algorithmically using bitwise operations. Logical bitwise operations are the fastest computations because processors natively support them. This approach generally leads to efficient solutions in problems where we can efficiently transform the input into its binary form or manipulate it directly at the bit level to produce the required output.
We’ll be focusing on performing bitwise operations on the following two categories of binary numbers:
Unsigned binary numbers: These numbers represent nonnegative integers.
Signed binary numbers: Signed binary numbers represent both positive and negative integers. They include a sign bit (such as the leftmost bit in
) to indicate the sign of the number.two’s complement representation A way of representing signed binary numbers, where the leftmost bit inidicates whether the binary number is positive (0) or negative (1).
A bitwise operation works on a
Logical NOT: This is a
that flips the value of the bit. If the bit isunary operator An operator that operates on a single operand. , we flip it to change a and vice versa. Below is an example of how this operator works on an unsigned binary numeral: