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

BITWISE OPERATOR by Sharin Aloma:-: Below

This document discusses bitwise operators in C programming. It defines the operators ~ (one's complement), << (left shift), >> (right shift), & (bitwise AND), | (bitwise OR), and ^ (bitwise XOR). It provides truth tables to explain the behavior of each operator when applied to binary bits. It also includes examples of code using the bitwise operators to manipulate integer values at the bit level.

Uploaded by

Shubham Meshram
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

BITWISE OPERATOR by Sharin Aloma:-: Below

This document discusses bitwise operators in C programming. It defines the operators ~ (one's complement), << (left shift), >> (right shift), & (bitwise AND), | (bitwise OR), and ^ (bitwise XOR). It provides truth tables to explain the behavior of each operator when applied to binary bits. It also includes examples of code using the bitwise operators to manipulate integer values at the bit level.

Uploaded by

Shubham Meshram
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

BITWISE OPERATOR by sharin aloma:-

One of C’s powerful features is a set of bit


manipulation operators.These permit the
programmer to access and manipulate
individual bits within a piece of data.The various
Bitwise Operators available in C are shown in
figure below:

OPERATOR MEANING
~ One’s compliment
>> Right shift
<< Left shift
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
These operators can operate upon ints and chars
but not on floats and doubles. Before moving on to
the details of the operators, let us first take a look at
the bit numbering scheme in integers and
characters. Bits are numbered from zero onwards,
increasing from right to left as shown:
7 6 5 4 3 2 1 0

Characters

One’s Compliment Operator:


On taking one’s compliment of a number, all 1’s
present in the number are changed to 0’s and all 0’s
are changed to 1’s.For example, one’s compliment
of 1010 is 0101.
Note that, here when we talk of a number, we are
talking of binary equivalent of the number. Thus,
one’s compliment of 65 means one’s compliment of
0000 0000 0100 0001, which is binary equivalent of
65.One’s compliment of 65 therefore would be,1111
1111 1011 1110.One’s compliment operator is
represented by the symbol ~(tilde).

Right Shift Operator:


The right shift operator is represented by >>.It needs
two operands. It shifts each bit in its left operand to
the right. The number of places the bits are shifted
depends on the number following the operator(i.e.its
right operand).
Thus, ch>>3 would shift all bits in ch three places to
the right. Similarly, ch>>5 would shift all bits 5
places to the right.
For example, if the variable ch contains the bit
pattern 11010111, then, ch>>1 would give
01101011 and ch>>2 would give 00110101.
Void main ()
{
Int a=3, b=4,c,d,e;
Clrscr ();
C=a>>2;
Printf (“c=%d”, c);
Getch();
}
OUTPUT: c=0

Left Shift Operator:


This is similar to the right shift operator,the only
difference being that the bits are shifted to the
left,and for each bit shifted,a 0 is added to the right
of the number.
Main()
{
Int a=3,b=4,c,d,e;
Clrscr();
C=a<<2;
Printf(“c=%d”,c);
Getch();
}
OU | 0 1
& 00 10 1
TP
1 1 1
UT: 0 0 0
c=1
1 0 1
2

Bitwise AND Operator:


This operator is represented as &.Remember it is
different than &&,the logical operator.The & operator
Operates on two operands.While operating upon
these two operands they are compared on a bit-by-
bit basis.Hence both the operands must be of the
same type(either char or int).This can be
represented in a more understandable form as a
‘Truth Table’-

Bitwise OR Operator:
Another important bitwise operator is the OR
operator which is represented as |.The rules that
govern the value of the resulting bit obtained after
ORing of two bits is shown in the truth table below:
Bitwise XOR Operator:
The XOR operator is represented as ^ and is also
called an Exclusive OR operator.The OR operator
returns 1,when any one of the two bits or both the
bits are 1,whereas XOR returns 1 only if one of the
two bits is 1.The truth table for the XOR operator is
given below:

^ 0 1
0 0 1
1 1 0

XOR operator is used to toggle a bit ON or OFF.A


number XORed with another number twice gives the
Original number.This is shown in the following
program.

#include<stdio.h>
#include<conio.h>
Void main()
{
Int b=50;
b=b^12;
printf(“\n%d”,b); /*this will print 62*/

b=b^12;
printf(“\n%d”,b);/*this will print 50*/
getch();
}

You might also like