C 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 –
res = num1 + num2;
In the above line all are integers which requires two/four/eight bytes memory in C
programming depending on the compiler.
Bit Level Operations
1. Sometimes it become mandatory to consider data at bit level.
2. We have to operate on the individual data bit. We also need to turn on/off
particular data bit during source code drafting, at that time we must use bitwise
operator for doing our task easier.
3. C Programming provides us different bitwise operators for manipulation of
bits.
4. Bitwise operators operates on Integer,character but not on float,double
5. Using bitwise operators we can manipulate individual bits easily
6. C programming supports 6 bitwise operators –
List of bitwise operators
Operator Name of Operator
~ One’s Compliment
>> Right Shift
<< Left Shift
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
Bitwise operator rules
Input Bits AND OR XOR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Summary :
Operator Usage
One's compliment operator Used to turn a bit on/off
AND bitwise operator Used to mask particular part of byte
Left shift bitwise operator Used to shift the bit to left
Right shift bitwise operator Used to shift the bit to right
C Bitwise Right Shift : (>>) Operator
In the previous chapter we have learnt about Bitwise Left Shift Operator. In this
chapter we are looking into Bitwise Right Shift Operator.
Bitwise Right Shift Operator in C
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
Quick Overview of Right Shift Operator
Original Number A 0000 0000 0011 1100
Right Shift by 2 0000 0000 0000 1111
Leading 2 Blanks Replaced by 0 ,Shown in RED
Direction of Movement of Data Right ========>>>>>>
Syntax :
[variable]>>[number of places]
Live Example : Bitwise Operator [Right Shift Operator]
#include<stdio.h>
int main()
{
int a = 60;
printf("\nNumber is Shifted By 1 Bit : %d",a >> 1);
printf("\nNumber is Shifted By 2 Bits : %d",a >> 2);
printf("\nNumber is Shifted By 3 Bits : %d",a >> 3);
return(0);
}
Output :
Number is Shifted By 1 Bit : 30
Number is Shifted By 2 Bits : 15
Number is Shifted By 3 Bits : 7
C bitwise left shift operator
Bitwise Left Shift Operator in C
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
Quick Overview of Left Shift Operator
Original Number A 0000 0000 0011 1100
Left Shift 0000 0000 1111 0000
Replaced by 0
Trailing Zero’s
(Shown in RED)
Direction of Movement of Data <<<<<=======Left
Syntax : Bitwise Left Shift Operator
[variable]<<[number of places]
Live Example : Bitwise Operator [Left Shift Operator]
#include<stdio.h>
int main()
{
int a = 60;
printf("\nNumber is Shifted By 1 Bit : %d",a << 1);
printf("\nNumber is Shifted By 2 Bits : %d",a << 2);
printf("\nNumber is Shifted By 3 Bits : %d",a << 3);
return(0);
}
Output :
Number is Shifted By 1 Bit : 120
Number is Shifted By 2 Bits : 240
Number is Shifted By 3 Bits : 480
Explanation of Left Shift Binary Operator :
We know the binary representation of the 60 is as below –
0000 0000 0011 1100
Now after shifting all the bits to left towards MSB we will get following bit pattern
–
0000 0000 0011 1100 = 60
<< 1
-------------------
0000 0000 0111 1000 = 120
Observations and Conclusion :
Shfting Binary 1 to Left By ‘X’ Decimal Converted
Bits Value Value
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
7 Bits 2^7 128
8 Bits 2^8 256
C Bitwise shift negative number
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
In this example we know that –
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]
Some more examples
Program #1 : Right bitwise shift negative number
#include<stdio.h>
int main()
{
int a = -60;
printf("\nNegative Right Shift by 1 Bit : %d",a >> 1);
printf("\nNegative Right Shift by 2 Bits : %d",a >> 2);
printf("\nNegative Right Shift by 3 Bits : %d",a >> 3);
return(0);
}
Output :
Negative Right Shift by 1 Bit : -30
Negative Right Shift by 2 Bits : -15
Negative Right Shift by 3 Bits : -8
Program #2 : Right bitwise shift negative number
#include<stdio.h>
int main()
{
int a = -60;
printf("\nNegative Left Shift by 1 Bit : %d",a << 1);
printf("\nNegative Left Shift by 2 Bits : %d",a << 2);
printf("\nNegative Left Shift by 3 Bits : %d",a << 3);
return(0);
}
Output :
Negative Left Shift by 1 Bit : -120
Negative Left Shift by 2 Bits : -240
Negative Left Shift by 3 Bits : -480
C bitwise shifting inside printf
Previous Page
Next Page
Bitwise Shifting Long inside Printf :
1. L stands for Long.
2. 1L means “1” is represented in the Long and then we are using Bitwise
Shifting of the operators.
Guess the output of the following code ?
main()
{
int i;
for(i=0;i<5;i++)
printf("%d\n", 1L << i);
}
Output :
1
2
4
8
16
Explanation :
During First Loop
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
During 3rd Loop
1L << 2
---------------------------------------
0000 0000 0000 0000 0000 0000 0000 0100
100 in Decimal = 4
C 1’s Compliment Operator
Previous Page
Next Page
One’s Compliment Operator in C
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
Original Number A 0000 0000 0011 1100
One’sCompliment 1111 1111 1100 0011
Zero’s Are Changed to 1
One’s Are Changed to 0
Syntax :
~Variable_Name
Live Example : Negation Operator in C Programming
#include<stdio.h>
int main()
{
int a=10;
printf("\nNegation of Number 1 : %d",~a);
return(0);
}
Output :
Negation of Number 1 : -11