position: fixed; top: auto !important; margin-left: 112px;

Bitwise Operators in C

There are many Bitwise operators in C as defined below:

& (bitwise AND) :- Takes two numbers as operand and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

| (bitwise OR) :- Takes two numbers as operand and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.

^ (bitwise XOR) :- Takes two numbers as operand and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

<< (left shift) :- Takes two numbers, left shifts the bits of first operand, the second operand decides the number of places to shift.

>> (right shift) :- Takes two numbers, right shifts the bits of first operand, the second operand decides the number of places to shift.

~ (bitwise NOT) :- Takes one number and inverts all bits of it.

Rule :-

& = 1 & 1 is 1 else 0.

| = any of two bit is 1 then 1 else 0.

^ = if both bits are different then 1 else 0.

a>> 1 = shift the bits of a to right by 1 and replace the first bit from left by 0 .

a>> 2 = shift the bits of a to right by 2 and replace the first two bits from left by 0 .

a<< 1 = shift the bits of a to left by 1 and replace the last bit from right by 0 .

a<< 2 = shift the bits of a to left by 2 and replace the last two bits from right by 0 .

~a = replace 0 by 1 and 1 by 0.

Example :-

a= 5    =    00000101 
b= 9    =    00001001
—————————————————————
a&b     =    00000001     
a|b     =    00001101
a^b     =    00001100
a>>1    =    00000100
a>>1    =    00001010  
~a      =    11111010


Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.