Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long. In this article, we will see the basics of bitwise operators, and some useful tips for manipulating the bits to achieve a task. This article assumes that you know the basics of Truth Table for various operators.

C language supports the following bitwise operators.

- | – Bitwise OR
- & – Bitwise AND
- ~ – One’s complement
- ^ – Bitwise XOR
- << – left shift
- >> – right shift

Though we are calling it as a bitwise operators, it always operate on one or more bytes i.e, it will consider the whole representation of the number when applying bitwise operators. By using some techniques, we can manipulate a single bit on the whole representation of the number as we will see in later sections

### Bitwise OR – |

Bitwise OR operator | takes 2 bit patterns, and perform OR operations on each pair of corresponding bits. The following example will explain it.

1010 1100 -------- OR 1110 --------

The Bitwise OR, will take pair of bits from each position, and if any one of the bit is 1, the result on that position will be 1. Bitwise OR is used to Turn-On bits as we will see in later sections.

### Bitwise AND – &

Bitwise AND operator &, takes 2 bit patterns, and perform AND operations with it.

1010 1100 ------- AND 1000 -------

The Bitwise AND will take pair of bits from each position, and if only both the bit is 1, the result on that position will be 1. Bitwise AND is used to Turn-Off bits.

### One’s Complement operator – ~

One’s complement operator (Bitwise NOT) is used to convert each “1-bit to 0-bit” and “0-bit to 1-bit”, in the given binary pattern. It is a unary operator i.e. it takes only one operand.

1001 NOT ------- 0110 -------

### Bitwise XOR – ^

Bitwise XOR ^, takes 2 bit patterns and perform XOR operation with it.

0101 0110 ------ XOR 0011 ------

The Bitwise XOR will take pair of bits from each position, and if both the bits are different, the result on that position will be 1. If both bits are same, then the result on that position is 0.

### Left shift Operator – <<

The left shift operator will shift the bits towards left for the given number of times.

int a=2<<1;

Let’s take the binary representation of 2 assuming int is 1 byte for simplicity.

Position7 6 5 4 3 2 1 0Bits0 0 0 0 0 0 1 0

Now shifting the bits towards left for 1 time, will give the following result

Position7 6 5 4 3 2 1 0Bits0 0 0 0 0 1 0 0

Now the result in decimal is 4. You can also note that, 0 is added as padding the in the position 0.

If you left shift like 2<<2, then it will give the result as 8. Therefore left shifting 1 time, is equal to multiplying the value by 2.

### Right shift Operator – >>

The right shift operator will shift the bits towards right for the given number of times.

int a=8>>1;

Let’s take the binary representation of 8 assuming int is 1 byte for simplicity.

Position7 6 5 4 3 2 1 0Bits0 0 0 0 1 0 0 0

Now shifting the bits towards right for 1 time, will give the following result

Position7 6 5 4 3 2 1 0Bits0 0 0 0 0 1 0 0

Now the result in decimal is 4. Right shifting 1 time, is equivalent to dividing the value by 2.

### Note on shifting signed and unsigned numbers

While performing shifting, if the operand is a signed value, then arithmetic shift will be used. If the type is unsigned, then logical shift will be used.

In case of arithmetic shift, the sign-bit ( MSB ) is preserved. Logical shift will not preserve the signed bit. Let’s see this via an example.

#include<stdio.h> int main() { signed char a=-8; signed char b= a >> 1; printf("%d\n",b); }

In the above code, we are right shifting -8 by 1. The result will be “-4”. Here arithmetic shift is applied since the operand is a signed value.

#include<stdio.h> int main() { unsigned char a=-8; unsigned char b= a >> 1; printf("%d\n",b); }

Note: Negative number are represented using 2’s complement of its positive equivalent.

2's compliment of +8 is 1111 1000 Right shifting by 1 yields, 0111 1100 ( 124 in decimal )

The above code will result in 124 ( Positive value ). Here logical shift is applied since the operand is unsigned, and it won’t preserve the MSB of the operand.

In our next article in this series, we will see how we can use this bit-wise operators to perform various tasks like turning on/off a specific bit, finding odd/even number, etc.

{ 23 comments… add one }

Thanks!!. Nothing like a refresher course. I liked it…

I believe that the statement “if the operand is a signed value, then arithmetic shift will be used. If the type is unsigned, then logical shift will be used” is compiler specific. From the ISO-C spec:

The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2 . If E1 has a signed type and a negative value, the resulting value is implementation-defined.

Hi,

Thanks a lot….

please take us slowly into reversing, xor is good and please teach us stack and all internal stuff

Hi !

It could be helpful.

Hello, I am not a programmer but sort of understand truth tables, interesting though.

I have just started my CCNA recently and an article on subnetting would be great.

nice article.

+1 for Richard Funderburk for pointing out the spec.

In Richards comment there is small a error in dislplaying the expression “E1 / 2E2 ” . That is actually (E1)/ (2 power of E2) ( i.e E1 / 2 pow(E2)) . That looks like 2 multiplied by E2.

Hello, nice article.

Can’t wait for the next one.

Thanks!

thank you very much.

good one……

goods

Buddy is there a book or something which will explain more of this with some more examples stuff and easy to understand for a newbie. Thank you

its quite helpful

thanks

Thanks a lot………..

could u pls give me an examples with negative values shifting

im benifited a lot . thank u

could you give an example of the exclamation point (‘!’)

very good example to understand the behavior of shift operator(for >> and << operator)

Very helpful

Thanks

Hi,

In those ~9 example is wrong. U can code in c program and check. Its as below.

~1001 should be = 1010. For this operator we add 1 bit and make it negative.

why there is not Ex-AND & Ex-NAND?

Good work..

Nice article.

Thk u

Clearly understand abt bitwise operator.