๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ bitwise-operators-in-c-cpp
Bitwise Operators in C - GeeksforGeeks
In C, bitwise operators are used to perform operations directly on the binary representations of numbers.
Published ย  December 13, 2025
bitwise operations provided by the C programming language
In the C programming language, operations can be performed on a bit level using bitwise operators. Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Bitwise_operations_in_C
Bitwise operations in C - Wikipedia
1 day ago - In the C programming language, operations can be performed on a bit level using bitwise operators. Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR, NOT operators. Instead of performing on individual bits, ...
Discussions

How do bitwise operators work in C?
N = 5 = (101)2 Actually, with 32-bit ints: N = 5 = 00000000000000000000000000000101 So ~N = ~5 = 11111111111111111111111111111010 And when those bits are in a signed int, you have the value -6 More on reddit.com
๐ŸŒ r/learnprogramming
8
1
February 18, 2022
bit manipulation - Bitwise Reduction Operators in C - Stack Overflow
Are there unary bitwise reduction operators in C like there are in Verilog? Like in Verilog we have: $display (" & 4'b1001 = %b", (& 4'b1001)); and the output of the function a... More on stackoverflow.com
๐ŸŒ stackoverflow.com
bit manipulation - Bitwise operation |= in C - Stack Overflow
I am going through example code and found this operation: displayMap[x + (y/8)*LCD_WIDTH]|= 1 (shift by) shift; where byte shift = y % 8; I understand | operand and = but what are two of them More on stackoverflow.com
๐ŸŒ stackoverflow.com
udp - C Programming - Bitwise operators and knowing when to utilize - Stack Overflow
You use bitwise operators when you want to view a number as a collection of bits, rather than an integer. It's much easier to say "I want this bit-pattern shifted two bits to the left" than to create the mathematically equivalent operation. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_bitwise_operators.php
C Bitwise Operators
In C, bitwise operators let you work directly with the bits (the 1s and 0s) that make up numbers in binary form.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ cprogramming โ€บ c bitwise operators
C Bitwise Operators
June 10, 2012 - Additionally, the symbols ^ (XOR), << (left shift) and >> (right shift) are the other bitwise operators. Even though these operators work on individual bits, they need the operands in the form C data types or variables only, as a variable occupies ...
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ bitwise-operators
Bitwise Operators in C Programming
In the arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction, multiplication and division are done in bit-level. To perform bit-level operations in C programming, bitwise operators are used.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how do bitwise operators work in c?
r/learnprogramming on Reddit: How do bitwise operators work in C?
February 18, 2022 -

Hey all! I got another beginner question. So, I'm learning about bitwise operators from this site. Here's what it says about the bitwise not:

Bitwise NOT is an unary operator that flips the bits of the number i.e., if the ith bit is 0, it will change it to 1 and vice versa. Bitwise NOT is nothing but simply the oneโ€™s complement of a number. Lets take an example.
N = 5 = (101)2
~N = ~5 = ~(101)2 = (010)2 = 2

So, ~5 = 2. Now, when I try this in C:

#include <stdio.h>

int main(void) {

int c = 5;

c = ~c;

printf("%d", c);

return 0;

}

The output of the above program is -6. What am I doing wrong?

๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-language โ€บ c-bitwise-operators
C Bitwise Operators | Microsoft Learn
April 7, 2022 - Access to this page requires authorization. You can try changing directories. ... The bitwise operators perform bitwise-AND (&), bitwise-exclusive-OR (^), and bitwise-inclusive-OR (|) operations.
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ When-should-you-use-the-bitwise-operators-in-C
When should you use the bitwise operators in C? - Quora
Answer (1 of 2): When you need to perform operations on individual bits, of course. The results of the bitwise operators are not typically the same as the arithmetic equivalent. If you need to change the value of a single bit (fairly common when doing low-level programming of hardware registers, ...
Top answer
1 of 2
4

In C, assuming unsigned or twoโ€™s complement, !~x or ~x == 0 serves as a bitwise AND; it is 1 if and only if each bit of x is 1.

!!x or x != 0 serves as a bitwise OR; it is 1 if any bit of x is 1.

The negations, not properly called NAND or NOR since they do not apply a NAND or NOR in a bitwise fashion but rather apply a NOT to the bitwise AND or OR, are simply !!~x or ~x != 0 and !x or x == 0.

There is no bitwise XOR in the operations specified by the C standard. GCC has __builtin_parity which can provide this.

The above apply to the full width of x. Narrower widths can be implemented by setting the extra bits to identity elements (1 for AND, 0 for OR and XOR).

2 of 2
1

There are no dedicated operators for this, however in most cases you can achieve the same result using bitwise operators and casting the result to a bool, which effectively is a single bit. For example:

  • AND: bitwise invert, convert to bool, negate:
    bool and_reduction_4_bits(int n) {
        return !(~n & 0b1111); // C23 adds binary literals
    }
    
  • OR: just convert to bool
    bool or_reduction(int n) {
        return n; // works for any number of bits
    }
    

The tricky one is XOR reduction. This could be done if you have a way to count the number of bits set, and then just check if that number is odd. Some compilers provide a builtin popcount() function to do that. If not, you can create your own function using bit twiddling hacks.

  • XOR: count bits, check if odd
    bool xor_reduction(int n) {
        return popcount(n) & 1;
    }
    
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ csharp โ€บ language-reference โ€บ operators โ€บ bitwise-and-shift-operators
Bitwise and shift operators - perform boolean (AND, NOT, OR, XOR) and shift operations on individual bits in integral types - C# reference | Microsoft Learn
January 24, 2026 - For operands of the same enumeration type, a logical operation is performed on the corresponding values of the underlying integral type. For example, for any x and y of an enumeration type T with an underlying type U, the x & y expression produces the same result as the (T)((U)x & (U)y) expression. You typically use bitwise logical operators with an enumeration type that is defined with the Flags attribute.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ where to learn about bitwise operators and such?
r/C_Programming on Reddit: Where to learn about bitwise operators and such?
November 22, 2022 -

Super beginner question, I apologize but I often see other people's see code and I see a lot of, 0xFE, ox00001, or >~ and I just don't understand what these are and why we need them. I google for bitwise operators and so far what I understand is that they operate on each bit of a byte and i guess & ands a 0/1, | ors >> shifts right and such. but why do we need these in actual coding environments where using these things is better than using normal variables.

also what are 0xFE called and where can I learn about them?

Top answer
1 of 12
40
0xFE is hexadecimal. The digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. There are exactly 16 digits (hence "hexadecimal"). If you think about binary, if you have 4 binary digits, these can represent exactly 16 values. So, each hexadecimal digit maps nicely to 4 bits. 2 hexadecimal digits maps to exactly one byte. If you memorize the bit patterns for the 16 hex digits, then hexadecimal is a convenient way to type/read binary data. 0xFE is, in binary, 11111110 and since I'm familiar with hexadecimal, I didn't really have to think about it to know that. As for why you need to manipulate bits, there are a few reasons. One, some hardware register might define certain bits to do certain things, and you need to construct values to put in registers to make the right things happen on the hardware. Or, maybe a bunch of bits is a very compact way to represent a set of integers (1 = present, 0 = absent) or to represent, e.g. free or occupied array elements if you're writing your own fixed size allocator. There are undoubtedly many other reasons clever people can think of to use bitwise operations. Edit: an obvious one is e.g. the "flags" argument to open(2), which packs a bunch of boolean flags into a single integer parameter. As for where to learn about them, hexadecimal is easy enough, first column is binary, 2nd is corresponding hex digit: 0000 0x0 0001 0x1 0010 0x2 0011 0x3 0100 0x4 0101 0x5 0110 0x6 0111 0x7 1000 0x8 1001 0x9 1010 0xA 1011 0xB 1100 0xC 1101 0xD 1110 0xE 1111 0xF As for bitwise operations, there's a book called "Hacker's Delight" which has all manner of bit twiddling tricks in it.
2 of 12
26
The OG guide for bit twiddling and why to use it: https://graphics.stanford.edu/~seander/bithacks.html
๐ŸŒ
Hero Vired
herovired.com โ€บ home โ€บ learning-hub โ€บ blogs โ€บ bitwise-operators-in-c
Bitwise Operators in C - Examples and Applications | Hero Vired
April 18, 2024 - Bitwise operators in C are special operators that perform operations at the binary level, working with individual bits of data rather than the entire data. These operators can manipulate the individual bits of integral data types, such as integers ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ operators-in-c
Operators in C - GeeksforGeeks
... #include <stdio.h> int main() ... printf("!a: %d\n", !a); return 0; } ... The Bitwise operators are used to perform bit-level operations on the operands....
Published ย  November 1, 2025
Top answer
1 of 4
172

Numbers can be expressed in binary like this:

3    = 000011
5    = 000101
10   = 001010

...etc. I'm going to assume you're familiar with binary.

Bitwise AND means to take two numbers, line them up on top of each other, and create a new number that has a 1 where both numbers have a 1 (everything else is 0).

For example:

    3          =>  00011
  & 5          =>  00101
------           -------
    1              00001

Bitwise OR means to take two numbers, line them up on top of each other, and create a new number that has a 1 where either number has a 1 (everything else is 0).

For example:

    3          =>  00011
  | 5          =>  00101
------           -------
    7              00111

Bitwise XOR (exclusive OR) means to take two numbers, line them up on top of each other, and create a new number that has a 1 where either number has a 1 AND the other number has a 0 (everything else is 0).

For example:

    3          =>  00011
  ^ 5          =>  00101
------           -------
    6              00110  

Bitwise NOR (Not OR) means to take the Bitwise OR of two numbers, and then reverse everything (where there was a 0, there's now a 1, where there was a 1, there's now a 0).

Bitwise NAND (Not AND) means to take the Bitwise AND of two numbers, and then reverse everything (where there was a 0, there's now a 1, where there was a 1, there's now a 0).

Continuing: why does word &= 15 set all but the 4 rightmost bits to 0? You should be able to figure it out now...

     n          =>  abcdefghjikl
  & 15          =>  000000001111
------            --------------
     ?              00000000jikl

(0 AND a = 0, 0 AND b = 0, ... j AND 1 = j, i AND 1 = i, ...)

How is this useful? In many languages, we use things called "bitmasks". A bitmask is essentially a number that represents a whole bunch of smaller numbers combined together. We can combine numbers together using OR, and pull them apart using AND. For example:

int MagicMap = 1;
int MagicWand = 2;
int MagicHat = 4;

If I only have the map and the hat, I can express that as myInventoryBitmask = (MagicMap | MagicHat) and the result is my bitmask. If I don't have anything, then my bitmask is 0. If I want to see if I have my wand, then I can do:

int hasWand = (myInventoryBitmask & MagicWand);
if (hasWand > 0) {
  printf("I have a wand\n");
} else {
  printf("I don't have a wand\n");
}

Get it?

EDIT: more stuff

You'll also come across the "bitshift" operator: << and >>. This just means "shift everything left n bits" or "shift everything right n bits".

In other words:

1 << 3 = 0001 << 3 = 0001000 = 8

And:

8 >> 2 = 01000 >> 2 = 010 = 2

2 of 4
2

"Bit" is short for "binary digit". And yes, it's a 0 or 1. There are almost always 8 in a byte, and they're written kinda like decimal numbers are -- with the most significant digit on the left, and the least significant on the right.

In your example, w1 & 3 masks everything but the two least significant (rightmost) digits because 3, in binary, is 00000011. (2 + 1) The AND operation returns 0 if either bit being ANDed is 0, so everything but the last two bits are automatically 0.

๐ŸŒ
SkillVertex
skillvertex.com โ€บ blog โ€บ bitwise-operators-in-c-c
Bitwise Operators In C/C++
January 24, 2024 - The result is 1 if the two corresponding bits are different (one is 0 and the other is 1). << (Left Shift): It takes two numbers: the first operand has its bits left-shifted by the number of positions specified by the second operand. >> (Right Shift): Similarly, it takes two numbers, right-shifting the bits of the first operand by the number of positions indicated by the second operand. ~ (Bitwise NOT): This operator takes a single number and inverts (flips) all its bits.
Top answer
1 of 3
14

โ€œas concise as possibleโ€ is an extremely vague requirement for a quiz. Are you expected to do code golf? Does removing whitespace and parentheses make it more concise? Anyway, hereโ€™s one solution using arithmetic on the results of comparisons:

int cmp(int x, int y) {
    return (x > y) - (x < y);
}
2 of 3
9
  • If x == y then x - y == 0.
  • If x < y then x - y < 0
  • If x > y then x - y > 0

So we want to see if we can convert the 3 conditions described in the 3 bullet-points above into 3 single output values needed for your cmp function:

int cmp( int x, int y ) {
    return -1 if x < y
    return  0 if x == y
    return  1 if x > y
}

This can be redefined as:

int cmp( int x, int y ) return singleValue( x - y );

int singleValue( int diff ) {
    return -1 if diff < 0
    return  0 if diff == 0
    return  1 if diff > 0
}

Now consider (and assuming) the computer uses two's complement for 32-bit signed integers, aka int) then all negative values will have the most-significant bit (MSB, the 0th bit) set to 1.

For 32-bit integers, this means that the following expression is true for all negative numbers:

( anyNegativeNumber & 0x8000000 ) == 0x8000000

The inverse is true: all positive non-zero integers will have an MSB of 0. Finally, all zero values (int zero == 0) have all their bits set to 0.

( anyPositiveNumber & 0x8000000 ) == 0

If we look at the MSB (first-bit) in addition to checking if any other bits are 1, with the desired output from our singleValue function described above:

value | first bit | any other bits | desired output
    0 |         0 |              0 |           0b ( 0)
  122 |         0 |              1 |           1b ( 1)
 -128 |         1 |              0 | 11111...111b (-1)
 -123 |         1 |              1 | 11111...111b (-1)

We can create 0 and 1 directly from the input value by masking the bits, but -1 is a special case but we can handle that, like so:

int diff = x - y; // so only the 1st and last bits are set

If the 1st bit of the diff is set, then return -1. If diff value is 0 then return 0 Else return 1

return ( diff & 0x80000000 == 0x80000000 ) ? 0xFFFFFFFF : ( diff != 0 );

This can be compacted:

int diff;
return ( ( ( diff = x - y ) & 0x80000000 ) == 0x80000000 ) ? 0xFFFFFFFF : ( diff != 0 );

This is still using the == and != operators though... which can be eliminated by taking advantage of the fact a single-bit (nth place) value can be shifted n-bits right to convert it to a boolean value:

( diff = x - y ) >> 31 // evaluates to 1 if x < y, 0 if x == y or x > y

The diff != 0 bit can be eliminated by taking advantage of the fact that !!a is 1 for all non-zero values and 0 for zero values:

!diff  // evaluates to 1 if diff == 0, else 0
!!diff // evaluates to 0 if diff == 0, else 1

We can combine the two:

int diff;
return ( ( diff = x - y ) >> 31 ) ? -1 : !!diff;

This operation has a branch in it (?:) and a temporary variable (diff) but there is a branch-less version of the same function.

It can be seen that the three possible outputs are:

0xFFFFFFFF == 1111_1111 1111_1111 1111_1111 1111_1111 b
0x00000000 == 0000_0000 0000_0000 0000_0000 0000_0000 b
0x00000001 == 0000_0000 0000_0000 0000_0000 0000_0001 b

The >> operator has "sign extension" for signed values, which means:

1000 b >> 2 == 1110 b
0100 b >> 2 == 0001 b

So diff >> 31 will be 1111..1111 if the 0th bit is 1, otherwise is 0000..0000.

Each individual bit's value can be expressed as a function of diff:

a = ( diff >> 31 ) // due to sign-extension, `a` will only ever be either 1111..1111 or 0000..0000
b = !!diff         // `b` will only ever 1 or 0
c = a | b          // bitwise OR means that `1111..1111 | 0` is still `1111..1111` but `0000..0000 | 1` will be `0000..0001`.

Or just:

c = ( diff >> 31 ) | ( !!diff );

Substituting this into the expression above:

int diff = x - y;
return ( diff >> 31 ) | !!diff;

Or

int diff;
return diff = x - y, ( diff >> 31 ) | !!diff;

The comma operator must be used because C does not specify nor guarantee the evaluation order of binary operator operand expressions, but the evaluation order of the comma operator is.

As this is an inline function, and assuming we're okay with mutable arguments, then we can eliminate diff because we only use x or y once:

return x = x - y, ( x >> 31 ) | !!x;

Here's my test program and the output I get, using GCC:

#include <stdio.h>

int cmp(int x, int y) {

    return x = x - y, ( x >> 31 ) | !!x;
}

int main() {

    printf( "cmp( 1, 2 ) == %d\n", cmp( 1,2 ) );
    printf( "cmp( 2, 2 ) == %d\n", cmp( 2,2 ) );
    printf( "cmp( 2, 1 ) == %d\n", cmp( 2,1 ) );
}

Output:

cmp( 1, 2 ) == -1
cmp( 2, 2 ) == 0
cmp( 2, 1 ) == 1

Now this is not perfect because of problems of integer overflow if x and y are both large numbers and x is negative, e.g. (-4000000000) - (4000000000). Checking for this condition is possible but defeats the point of making the code as succinct as possible - you would also need to add code that handles the error condition too. In this situation, a better way would be to simply check the user-provided inputs instead of checking the function argument values.

TL;DR:

int cmp(int x, int y) {

    return x = x - y, ( x >> 31 ) | !!x;
}
๐ŸŒ
IDC Online
idc-online.com โ€บ technical_references โ€บ pdfs โ€บ information_technology โ€บ C_Bitwise_Operators.pdf pdf
C Bitwise Operators ๏‚ท
In the result if the 3rd bit is 1 then the 3rd bit in the original number is ON. if its 0 then the ... Result is -->0000 0100. The result has 3rd bit as 1. So the 3rd bit on the original number is set as ON. ... The Bitwise OR operator is also a binary operator so it requires 2 operands. ... Example: consider a=10 and b=6.