Bitwise Operations Basics: AND, OR, XOR, NOT, Shifts, and Masks Explained
Bitwise operations are the most fundamental calculations a computer performs, working on numbers directly as sequences of 0s and 1s. This article explains the AND, OR, XOR, and NOT logic operations, left and right shifts, techniques for setting and clearing specific bits, masks and flag management, the link to powers of two, and practical uses like permissions, color, and optimization.
While learning to program, you may run into unfamiliar symbols like &, |, ^, and <<. These are bitwise operations, calculations that manipulate a number directly as a sequence of 0s and 1s. They require a slightly different way of thinking than ordinary addition or multiplication, but once you understand the mechanism they become a powerful tool for permission management, storing flags, and speeding things up. This article explains the basics of bitwise operations step by step.
What Are Bitwise Operations
Computers handle every number in binary, that is, as a sequence of 0s and 1s. For example, the decimal number "13" is "1101" in binary. Each of these digits is called a bit. A bitwise operation is a calculation that processes this sequence of bits directly, digit by digit.
The addition and multiplication we use every day deal with "the size of the whole number," but bitwise operations focus on the individual bits themselves: which digits are 0 and which are 1. That can feel unintuitive at first. The shortcut is to learn while checking real values, and reading along while converting decimal to binary in the Number Base Converter makes the correspondence between digits easy to picture.
Bitwise operations shine when you want to save memory, manage many on/off states together, or make processing faster. Because you can pack dozens of "yes/no" answers into a single integer, you can represent data extremely compactly, which is a major advantage.
The Four Logic Operations: AND, OR, XOR, NOT
At the heart of bitwise operations are the four logic operations AND, OR, XOR, and NOT. Each compares corresponding digits one bit at a time to decide the result. Let us look at how each behaves with a truth table.
| A | B | AND (A&B) | OR (A|B) | XOR (A^B) |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
- AND (
&): results in 1 only when both bits are 1. It checks "do both hold," and it is the star of the mask processing described later. - OR (
|): results in 1 if either bit is 1. It is used to set (turn on) multiple flags at once. - XOR (exclusive OR,
^): results in 1 only when the two bits differ. It can judge "same or different" and is handy for flipping or swapping bits. - NOT (
~): flips every bit of a single value. 1 becomes 0 and 0 becomes 1.
For example, AND of 12 (1100 in binary) and 10 (1010) leaves only the digits where both are 1, giving 1000, which is 8. OR of the same two values gives 1110, or 14, and XOR gives 0110, or 6. Until you get used to it, writing the binary on paper and aligning the digits helps; to check your answers, the Bitwise Calculator instantly shows the result of an operation in both binary and decimal.
Left and Right Shifts: The Link to Powers of Two
Alongside the logic operations, shift operations are also important. They slide the entire sequence of bits left or right, much like moving digits up and down in the decimal world.
A left shift (<<) slides bits to the left and fills the freed right side with 0s. This is equivalent to doubling the number. Shift one bit left and you multiply by 2, two bits by 4, n bits by 2 to the power n. For example, 3 << 2 shifts 3 (11 in binary) two digits left to 1100, which is 12 (3 x 4 = 12).
A right shift (>>) does the opposite, sliding bits to the right. This is equivalent to dividing by 2 and discarding the fraction. 13 >> 1 shifts 13 (1101) one digit right to 110, which is 6 (13 / 2 = 6.5, truncated).
Here you can see the deep link between bitwise operations and powers of two. Each bit carries a power-of-two weight: 1, 2, 4, 8, 16, and so on. Writing 1 << n gives "a value where only the n-th bit from the bottom is 1," that is, 2 to the power n. This property is the foundation for the bit-manipulation techniques explained next. To understand what value a shift produces, comparing the binary display in the Number Base Converter makes it clear at a glance.
Setting, Clearing, and Toggling Bits
The true power of bitwise operations is the ability to freely manipulate just one specific bit inside an integer. Here are three representative operations, each built by combining a value where only the target bit is 1 (1 << n).
- Set a bit (turn on):
value | (1 << n). Using OR, set the n-th bit to 1. The other bits are preserved. - Clear a bit (turn off):
value & ~(1 << n). AND with a mask where only the n-th bit is 0 and all others are 1, setting the n-th bit to 0. - Toggle a bit:
value ^ (1 << n). Using XOR, switch the n-th bit from 1 to 0 or 0 to 1.
Furthermore, to check whether a specific bit is set, compute value & (1 << n); if the result is not 0, that bit is 1. The key point is that none of these affect any bit other than the target. When you want to confirm the behavior with real values, try AND, OR, and XOR in the Bitwise Calculator and observe which digits changed.
Masks and Flag Management
Combining the techniques so far lets you manage multiple on/off states together in a single integer. This "pattern for extracting or manipulating only the target bits" is called a mask (bitmask).
For example, suppose a feature has three switches: "notifications on," "dark mode," and "autosave." If you assign each the power-of-two values 1, 2, and 4, you can combine the three states into one integer. If only "notifications on (1)" and "autosave (4)" are enabled, OR them together: 1 | 4 = 5. Just by storing this value of 5, you can represent all three settings.
To read the states back, use AND. 5 & 2 is 0, so "dark mode is off," while 5 & 4 is nonzero, so "autosave is on." Assigning a power of two to each state, setting with OR, and checking with AND is the basic form of flag management. Because you can collapse a database column or config setting into a single number, you save a great deal of storage. When you want to check several masks at once, the Bitwise Calculator is handy: lining up the values in binary lets you grasp intuitively which flags are set.
What They Are Used For: Permissions, Color, Optimization
Bitwise operations may look abstract, but they are used throughout real software. Here are three representative uses.
1. Permission management
The most famous example is file and user permission management. The Unix-style file permissions "read (4)," "write (2)," and "execute (1)" are exactly flags assigned powers of two. "Read plus write" is represented as a single number, 4 + 2 = 6. Web app role management often uses the same idea, holding permissions like "view," "edit," and "delete" as bits.
2. Representing color (RGBA)
Screen colors are often handled by representing the red, green, blue, and alpha (transparency) components as values from 0 to 255 and packing them into a single integer. Since each component occupies 8 bits, shifts and masks let you extract a specific color component from one integer or, conversely, assemble the components. Extracting the red component with (color >> 16) & 0xFF is a typical example.
3. Optimization and speed
Because bitwise operations are very light for a CPU, they are also used to speed things up. For instance, "shift right by one bit" can be faster than "divide by 2," and an AND can be faster than "the remainder of dividing by a power of two." Also, collapsing many on/off values into a single integer greatly reduces memory usage and access counts. Such optimizations are still in active use in performance-sensitive fields like games, embedded devices, and large-scale data processing.
Bitwise operations rest on the simple idea of viewing a number as a sequence of 0s and 1s and operating on it digit by digit. Once you have the four operations AND, OR, XOR, and NOT plus shifts down, you can apply them broadly, from flag management to permissions, color, and optimization. Start by trying actual operations in the Bitwise Calculator, and confirm the binary display in the Number Base Converter to understand how the bits move, with your own eyes.
Please note that this article is intended to provide general information. Shift behavior (such as the difference between signed and unsigned) and operator specifications differ in detail depending on the programming language you use. When implementing, always check the latest official documentation and language specification.
FAQ
What are bitwise operations?
Bitwise operations are calculations that view a number in binary (a sequence of 0s and 1s) and process it directly, digit (bit) by digit. The main ones are the AND, OR, XOR, and NOT logic operations and left and right shifts. They are widely used to pack multiple on/off states into a single integer and to speed up processing.
How do I choose between AND, OR, and XOR?
AND results in 1 only when both bits are 1 and is used to extract specific bits (masking) and check states. OR results in 1 if either bit is 1 and is used to set (turn on) flags. XOR results in 1 when the two bits differ and is handy for flipping (toggling) or swapping bits.
What do left and right shifts do?
A left shift (<<) slides bits to the left and multiplies the number by 2; shifting by n bits multiplies by 2 to the power n. A right shift (>>) slides bits to the right, dividing by 2 and truncating. Because each bit carries a power-of-two weight, shifts can be a fast alternative to multiplication and division.
What are bitmasks and flag management?
It is a technique of assigning each state a power-of-two value (1, 2, 4, 8, and so on), setting multiple states into one integer with OR, and checking individual states with AND. This lets you express many on/off settings with a single number and save storage. Unix file permissions are a representative example.
Does the WAZA TOOLS Bitwise Calculator store data?
No. The WAZA TOOLS Bitwise Calculator and Number Base Converter run entirely in your browser, so the values you enter are never sent to or stored on a server. They work on smartphones too, and you can use them as many times as you like, for free.