0% found this document useful (0 votes)
3 views

Bitwise Operators.data pro

The document explains six bitwise operators in C: right shift (>>), left shift (<<), bitwise OR (|), bitwise AND (&), bitwise XOR (^), and one's complement (~). It provides examples of how these operators function with binary numbers and includes sample C programs demonstrating the use of bitwise shift, AND, OR, and XOR operations. The operators can only be applied to integer types, not floating-point numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Bitwise Operators.data pro

The document explains six bitwise operators in C: right shift (>>), left shift (<<), bitwise OR (|), bitwise AND (&), bitwise XOR (^), and one's complement (~). It provides examples of how these operators function with binary numbers and includes sample C programs demonstrating the use of bitwise shift, AND, OR, and XOR operations. The operators can only be applied to integer types, not floating-point numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Bitwise Operators:-

C supports six bit type operators. These operators can operates only on an integer
operators (such as char, short, long int) but not on float.
Bitwise Operators
Operator Meaning
>> Right Shift
<< Left Shift
| Bitwise OR
& Bitwise AND
^ Bitwise xor(Exclusive or)
~ One's Complement
Right shift:- The right shift operator, shift each in the operand to the right. The number of
places the bits are shifted is determined by the number given after the right shift Operator.
Example If the variable 'A' Constains 1100101, then the operation A>>1 whould result in
0110010 stroed in A. if the variable 'A' Contains 1100101, then the operation A>>3 would
result in 00011001 stored in A.
Left shift:- The left shift operator, shifts each bit in the operand to left. The number of
places the bits are shifted id Determined by the number given after the left shift operator.
Example if the variable 'B' contains 11001, then the operation 'B'<<2 would result in 1100100.
Bitwise AND Operator(&):- Bitwise AND Operator is represented by single ampersand symbol.
The & operator operates on two operands and compare them by bit by bit. The & Operator
operates on a pair of bits to yeild resultant bit.
First Bit Second Bit First & Second Bit
0 0 0
1 0 0
0 1 0
1 1 1

Bitwise OR Operator (|):- Bitwise OR operator is another C operator that works on two
operands. It is represented by pipe '|' Symbol.
First Bit Second Bit First & Second Bit
0 0 0
1 0 1
0 1 1
1 1 1
Bitwise Exclusive Or (XOR) operator (^):-
Bitwise XOR operator is an extention of bitwise Or Operator
and is represented by Caret '^' Symbol.
First Bit Second Bit First & Second Bit
0 0 0
1 0 1
0 1 1
1 1 0
For Example 127 ^ 120 is
01111111 127 in binary
01111000 120 in Binary

XOR Result 00000111


*/

//program on Bitwise Right shift and Left shift operators


#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
clrscr();
printf("\n a=%d",a);
printf("\n a<<3=%d",a<<3);
printf("\n a<<1=%d",a<<1);
printf("\n a=a<<2=%d",a=a<<1);
printf("\n a=a<<3=%d",a=a<<3);
printf("\n a>>2=%d",a>>1);
printf("\n a>>3=%d",a>>3);
printf("\n a=a>>2=%d",a=a>>3);
printf("\n a=a>>3=%d",a=a>>1);
printf("\n a=%d",a);
getch();
}
*/

/*program on Bitwise Right shift and Left shift operators


#include<stdio.h>
#include<conio.h>
void main()
{
int a=18;
clrscr();
printf("\n a=a<<3=%d", a<<3);
printf("\n a<<3=%d", a<<3);
printf("\n a=a<<2=%d", a=a<<2);
printf("\n a<<3=%d", a<<3);
printf("\n a=a<<3=%d", a=a<<3);
printf("\n a=a>>2=%d", a>>2);
printf("\n a>>3=%d", a>>3);
printf("\n a=a>>2=%d", a=a>>2);
printf("\n a>>3=%d", a>>3);
getch();
}
*/

//Program on Bitwise AND(&) and OR(|) and XOR(^) operators


#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=15;
clrscr();
printf("\n %d & %d (a&b)=%d",a,b,a&b);
printf("\n %d | %d (a|b)=%d",a,b,a|b);
printf("\n %d ^ %d (a^b)=%d",a,b,a^b);
a=57,b=20;
printf("\n %d & %d (a&b)=%d",a,b,a&b);
printf("\n %d | %d (a|b)=%d",a,b,a|b);
printf("\n %d ^ %d (a^b)=%d",a,b,a^b);
//clrscr();
getch();
}

You might also like