C Programming Bitwise Operators
C Programming Bitwise Operators
This tutorial will explain you the basic idea behind C Programming Bitwise
Operators. Why bitwise operators are introduced in C Programming.
C Programming Bitwise Operators
Byte Level Operations
Till now we have seen all the operators uses variables and constants for expression
solving and they operate all the calculations at byte level. like –
In the above line all are integers which requires two/four/eight bytes memory in C
programming depending on the compiler.
Bit Level Operations
~ One’s Compliment
| Bitwise OR
^ Bitwise XOR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Summary :
Operator Usage
1. It is denoted by >>
2. Bit Pattern of the data can be shifted by specified number of Positions to
Right
3. When Data is Shifted Right , leading zero’s are filled with zero.
4. Right shift Operator is Binary Operator [Bi – two]
5. Binary means , Operator that require two arguments
Syntax :
[variable]>>[number of places]
#include<stdio.h>
int main()
{
int a = 60;
return(0);
}
Output :
1. It is denoted by <<
2. Bit Pattern of the data can be shifted by specified number of Positions to
Left
3. When Data is Shifted Left , trailing zero’s are filled with zero.
4. Left shift Operator is Binary Operator [Bi – two]
5. Binary means , Operator that require two arguments
Replaced by 0
Trailing Zero’s
(Shown in RED)
[variable]<<[number of places]
#include<stdio.h>
int main()
{
int a = 60;
return(0);
}
Output :
Now after shifting all the bits to left towards MSB we will get following bit pattern
–
0 Bit 2^0 1
1 Bit 2^1 2
2 Bits 2^2 4
3 Bits 2^3 8
4 Bits 2^4 16
5 Bits 2^5 32
6 Bits 2^6 64
Previous Page
Next Page
In this tutorial we will be learning how to bitwise shift negative number using left
and right shift. Tutorial explains everything about negative bitwise shifting.
Bitwise shift negative number
We can Also Shift Negative Number using bitwise operators provided by C (Left
Shift and Right Shift). We will learn how to use right shift or left shift operators for
Negative Number.
Recommended article : Bitwise left shift | Bitwise right shift
Program #1 : Simple Bitwise shift negative number
#include<stdio.h>
void main()
{
printf("%x",-1<<4);
}
Output :
fffffff0
Explanation
1. Internal representation of -1 is all 1’s 1111 1111 1111 1111 1111 1111 1111
1111 in an 32 bit compiler.
2. When we bitwise shift negative number by 4 bits to left least significant 4 bits
are filled with 0’s
3. Format specifier %x prints specified integer value as hexadecimal format
4. After shifting 1111 1111 1111 1111 1111 1111 1111 0000 = FFFFFFF0 will
be printed.
After 1st Shift : [1111 1111 1111 1111 1111 1111 1111 1110]
After 2nd Shift : [1111 1111 1111 1111 1111 1111 1111 1100]
After 3rd Shift : [1111 1111 1111 1111 1111 1111 1111 1000]
After 4th Shift : [1111 1111 1111 1111 1111 1111 1111 0000]
#include<stdio.h>
int main()
{
int a = -60;
return(0);
}
Output :
Negative Right Shift by 1 Bit : -30
Negative Right Shift by 2 Bits : -15
Negative Right Shift by 3 Bits : -8
#include<stdio.h>
int main()
{
int a = -60;
return(0);
}
Output :
Previous Page
Next Page
Bitwise Shifting Long inside Printf :
main()
{
int i;
for(i=0;i<5;i++)
printf("%d\n", 1L << i);
}
Output :
1
2
4
8
16
Explanation :
1L << 0
---------------------------------------
0000 0000 0000 0000 0000 0000 0000 0001
During 2nd Loop
1L << 1
---------------------------------------
0000 0000 0000 0000 0000 0000 0000 0010
10 in Decimal = 2
1L << 2
---------------------------------------
0000 0000 0000 0000 0000 0000 0000 0100
100 in Decimal = 4
Previous Page
Next Page
1. It is denoted by ~
2. Bit Pattern of the data can be Reversed using One’s Compliment
3. It inverts each bit of operand .
4. One’s Compliment is Unary Operand i.e Operates on 1 Argument
Syntax :
~Variable_Name
#include<stdio.h>
int main()
{
int a=10;
return(0);
}
Output :