54 Programming and Data Structures
3.16 Write a program to display 1 if inputed number is except 100 otherwise 0. Use the
logical NOT operator (!).
xnain()
{
in t x,z;
c l r s c r () ;
printf ("Enter number:");
scanf ("%d", &cx);
z=(xl=100 ? 1 : 0);
printf ("d :%d", z);
}
OUTPUT : Enter number : 100
d : 0
Enter number: 10
d : 1
Explanation In the above program ! NOT operator is used with the conditional operator. The
value of x is entered. Here, if the value of x is other than 100 then 1 is assigned to z otherwise 0.
3.7 BITWISE OPERATORS
C supports a set of bitwise operators as listed in the Table 3.6. C supports six bit operators. These
operators can operate only on an integer operands such as i n t , ch a r, sh o r t/ long in t etc.
Table 3.6 Bitwise operators
Operator
Meaning
>>
Right shift
<<
Left shift
A
Bitwise xor (Exclusive OR)
-
One's complement
&
Bitwise AND
1
Bitwise OR
3.17 Write a program to shift inputed data by two bits right.
m ain()
{
in t x»y ;
c l r s c r ( ) ;
printf ("Read The Integer from keyboard (x)
Operators and Expressions 55
scanf ("%d", &x);
x » = 2 ;
printf ("The Right shifted data is = %d",y);
i
OUTPUT: Read The Integer from keyboard (x) 8
The Right shifted data is a 2
Before the execution of the program: - The number entered through the keyboard is 8 and its
corresponding binary number is 1 0 0 0.
0 0 0 0 0000 00
00 10 00
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 0
After execution of the program: - as per the above program the inputed data x is to be shifted by 2
bits right side. The answer in binary bits would be as follows.
0 0 0 0 0 0 0 0 0 0
0 0
0 0 1
0
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 0
Shifting two bits right means the inputted number is to be divided by 2s where s is number of shifts
i.e. in short y=n/2*
Where, n = Number
a = Number of position to be shifted.
As per the program as cited above
Y= 8/22 = 2 .
Similarly, a program for shifting to the left can be written as follows.
3.18 Write a program to shift inputed data by two bits left.
mainO
{
int x,y;
c lr s c r ();
printf ("Read The Integer from keyboard (x)
scanf ("%d”, &x);
x « = 3 ;
y=x;
printf ("The Bight shifted data is = %d",y);
i
OUTPUT : Read The Integer from keyboard (x)2
The Left shifted data is = 16
56 Programming and Data Structures
Before execution of the program: - The number entered through the keyboard is 2 and its
corresponding binary number is 1 0. The bits will be as follows.
00000000 00 00
00 10
15 14 13 12 11 10 09 08 07 06
05
04
03
02 01 0
After execution of the program: - As per the above program the
bits left side. The answer in binary bits would be as follows.
inputed data x is to be shifted by 3
00000000 00
01
00 0 0
15 14 13 12 11 10 09 08 07 06
05
04
03
02 01 0
The corresponding decimal number is 16 i.e. answer should be 16.
Shifting three bits left means number is multiplied by 8 in short y=n*2s
Where n = Number
s = Number of position to be shifted.
As per the program as cited above
Y=2*23 = 16.
3.19 Write a program to use bitwise AND operator between the two integers and display the
results.
m&in()
{
in t a,b,c;
c l r s c r ( ) ;
printf ("Read The Integers from keyboard (a & b)
scanf ("%d %d", &a,&b);
c-a & b;
printf ("The Answer after ANDing is (C)= %d",c);
I
OUTPUT:
Read The Integers from keyboard (a & b ): 8 4
The Answer after ANDing is (C) = 0
a=8
0000 0 00 0 0 0 001000
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 0
Before execution
0000 0 00 0 0 0 000100
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 0

Get Programming and Data Structures now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.