We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What are Bitwise Operators?

By Kurt Inman
Updated: May 16, 2024

In a computer program, bitwise operators are operators for manipulating bit patterns in expressions. Bitwise AND, OR, exclusive OR (XOR), NOT, right shift and left shift are common operations. Most computer programming languages include all six of these operators. They are often used for setting, clearing or flipping individual bits in programs which control computer hardware. Bitwise operators are also frequently used in data encryption and compression algorithms.

The common bitwise operators are usually represented symbolically in programming languages like C, C++ and Java. They are used in expressions just as arithmetic operators like plus or minus signs would be. The bitwise operator AND is represented with the ampersand &, OR with the pipe | and XOR with the caret ^. NOT, also known as the complement operator, is indicated with a tilde ~.

The right shift operator uses a double-caret pointing to the right >>. In contrast, left shift is indicated by a left-pointing double-caret <<. Java includes another right shift operation, shown with a right-pointing triple-caret >>>.

Bitwise operators apply a logical operation to each pair of bits in their operands. NOT, right shift and left shift have only one true operand; the second value in shift operations is the bit count. To better understand how bitwise operators work, an individual can visualize their operands as binary numerals. For example, the complement operator flips each bit of its single operand to the opposite state. A one becomes a zero and a zero becomes a one. The complement of the 8-bit binary value 00110101 is 11001010.

The AND operator has two operands. Bit by bit, a logical AND operation is performed using one corresponding bit from each operand. The outcome of each bit operation is placed in that bit position of the overall result. For example, the AND operator will process bit 7 of one operand with bit 7 of the other operand. The outcome will be stored in bit 7 of the overall result.

In an AND operation, both operand bits must be ones in order for the result to be one, otherwise the result is zero. For example, if the 8-bit binary values of the operands are 00110101 and 11110000, the result will be 00110000. A common use of the AND operator is to zero out particular bits in the result. This is done by placing zeroes in those bit positions in one of the operands.

In an OR operation, both operand bits must be zeroes in order for the result to be zero; otherwise, the result is one. A common use of the OR operator is to set certain bits of the result to ones. This is done by placing ones in those bit positions in one of the operands. For the XOR operation, the result is zero if both operand bits are zero or if both operand bits are one—otherwise, the result is one.

Left shift and right shift move the bits in the operand left or right by the specified number of bit positions. A logical right shift moves a zero into the leftmost bit as part of the shift. An arithmetic right shift copies the leftmost bit—the sign bit—before the shift into the same position after the shift. The original bit is also shifted right along with the rest. Either type of left shift moves a zero into the rightmost bit.

When an operand is right-shifted, the rightmost bit before the shift is simply thrown away. Similarly, the leftmost bit before a left shift is disposed of. It does not wrap around to the other end of the operand.

Bitwise shift operations are language- and implementation-dependent. For example, in C and C++, >> and << perform logical shifts if the operand is an unsigned integer. If the operand is a signed integer, it is likely that an arithmetic shift will be done instead. In Java, all operands are considered signed, and arithmetic shifts are always performed with >> and <<. The >>> operator is used for a logical right shift, but it is still possible to accidentally do an arithmetic right shift instead without careful typecasting.

Complications can also arise when operands have different bit lengths or when some are signed and some are unsigned. Bitwise operators and numeric constants in the middle of a complex expression may not be evaluated as anticipated. Care must be taken to specify the size and signed/unsigned nature of each quantity in the expression. This can be done with careful typecasting or interim assignments to specific variable types in program code.

EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Discussion Comments
Share
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.