Session 19 Bitwise Operators2
Session 19 Bitwise Operators2
2. Objectives
3. Learning Outcomes
4. Session Introduction
Concept of Bitwise operators like Left shift <<, Right shift >> ,bitwise complement ~
Procedure to solve the left, right shift operators and complement.
Formula to solve left, right shift operators and complement.
Some programs related to Bit wise operator.
How Negative numbers are represented 2’s complement.
5. Session Description
Bit operators works at the bit-level. we have some bit wise operators like below
(left shift) << in C requires two arguments; the first argument determines how many times
to left shift, and the second argument determines how many bits to left shift.
(right shift)>> in the C language that accepts two arguments, right shifts the first argument's
bits, and determines how many times to right shift using the second operand.
~ complement NOT in C takes one argument and flips all bits of it.
5<<2
5 denotes 00101 .
00101—5
01010 --- 1st time shift left. (Shift all bits to left side by one position)
5>>2
5>>2
5-----0101
0010---- 1st time right shift (shift all bits to right side by one position)
2’s Complement:
Two’s complement is operation on bits of specific number. The 2’s complement of a number
is equal to the complement of that number plus 1.
5---0101
1’s complement for 5 is 1010( flip bits 1 to 0 and 0 to 1)’
1s complement of 5 is 1010 plus 1
1010
1+
------------
1011----- decimal 11.
So 2’s complement 5 is 11.
Actual procedure
35----00100011
Complement of 35 means
But when you execute in compiler, we will get answer as -36. How???
-11011100 (binary representation of -220 here minus (-) Indicates sign bit MSB).
-00100011(flip 0 to 1 and 1 to 0)
1 +( add 1 to above)
-------------------
-00100100
=-36 (binary equivalent).
1. The left-shift and right-shift operators will not work with negative numbers. If you use
which gives wrong result.
2. If the number is shifted more than the size of the integer, the result is wrong For
3. Formula for left shift is 1<<7 = 1*pow (2,7)). formula for right shift is division of the first
argument and second argument raise to power of 2 (1>>8 = 1/pow(2,8))
#include <stdio.h>
int main ()
return 0;
}
Program to count the number of 1’s in given number if count is even the print ram wins or
ravi wins. ( Right shift example)
#include<stdio.h>
int onesCount(int n)
{
int onescount=0;
while(n>0)
{
if((n & 1)==1)
onescount++;
n=n>>1;
}
return onescount;
}
int main()
{
int num,ans;
printf("Enter Any Number=");
scanf("%d",&num);
ans=onesCount(num);
if(ans%2==0)
printf("Ram wins");
else
printf("Mouni wins");
return 0;
}
Group discussion: The session will conclude with a group discussion on the benefits and
types of bitwise operators in C and we ask student to practice on the different types of
bitwise operators.
Oral Quiz- asking Quick MCQ Question on bitwise operators by dividing the whole class in to
4 groups who get more score that team will win.
One minute Answer: ask the student to write concept within a minute on paper when
faculty tell a particular key word (Ex MSB).
7. Examples & Contemporary Extracts of Articles / Practices to convey the idea of the Session
We use the bitwise operators in C to perform operations numeric values at a bit level.
Performing a bitwise operation is also called bit-level programming. To perform numerical
computations for faster we use bitwise because it has only two digits 1 or 0.
https://www.programiz.com/c-programming/bitwise-operators
8. Table Numbering
--
--
4 Write formulas for left, right shift operators, bit wise complement.
11. Summary
Different types of bitwise operators and their usage. By using bitwise we perform arthimatic
operations at bit level. Operators are << left shift , >> right shift ,~ complement .
Shifts the bits of an integer to the left by a specified number of positions, effectively
multiplying the integer by 2 to the power of the shift amount.
Shifts the bits of an integer to the right by a specified number of positions, effectively
dividing the integer by 2 to the power of the shift amount.
12 Terminal Questions
3 implement a Code to find number of one’s and zero’s in given number using bit wise.
6 implement a Code on flipped bits using bitwise (Ex here a is 8 -- 1000 b is 15 ---1111 . how
many bits u have to flip to get b. a has to flipped 3 bits)
Encryption and compression: Image compression or data encryption algorithms like AES
and DES where we perform on bit wise.
The C Programming Language by Brian Kernighan and Dennis Ritchie 2nd edition pearson
publication - This is the classic book on C programming and is a great resource for learning
about functions in C.
C: THE COMPLETE REFERENCE McGraw Hill Education; 4th edition by Herb Scheldt.
https://www.codecademy.com/resources/docs/cpp/bitwise-operators
https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/
https://www.programiz.com/c-programming/bitwise-operators
15. Keywords
~ complement.
2’s complement
1’s complement
MSB
LSB
Sign Bit