Bit Manipulation
Bit Manipulation
Engineers
Bit Manipulation
ICEN 360– Spring 2017
Prof. Dola Saha
1
Bitwise Operation
Ø Computers represent all data internally as sequences of
bits.
Ø Each bit can assume the value 0 or the value 1.
Ø The bitwise operators are used to manipulate the bits of
integral operands both signed and unsigned.
Ø Unsigned integers are normally used with the bitwise
operators.
Ø Bitwise manipulations are machine dependent.
2
Bitwise Operator
3
Bitwise Operation Example
n=13 00001101
~n 11110010
00001101
n << 1
Lost 0 00011010 0 Inserted
4
Bitwise Operation Example
n 00001101
m 01010101
n&m
00000101
n 00001101
m 01010101
n|m 01011101
n 00001101
m 01010101
n^m 01011000
5
Display Bits Example (1)
6
Display Bits Example (2)
7
Bitwise Operation Example Code (1)
8
Bitwise Operation Example Code (2)
9
Bitwise Operation Example Code (3)
10
Bitwise Operation Example Code (4)
11
Bitwise Operation Example Code Output
12
Bitwise Operation Application:
TCP segment structure
32 bits
URG: urgent data counting
(generally not used) source port # dest port #
by bytes
sequence number of data
ACK: ACK #
valid acknowledgement number (not segments!)
head not
PSH: push data now Len (4) used
UAPR S F receive window
(generally not used) # bytes
checksum Urg data pointer rcvr willing
RST, SYN, FIN: to accept
options (variable length)
connection estab
(setup, teardown
commands)
application
Internet data
checksum (variable length)
(as in UDP)
13
Multiply and Divide by Bitwise Operation
Ø Left Shift
§ Multiply
Ø Right Shift
§ Divide
14
Revisiting Classwork Assignment
Ø Write a program to generate data for N students. Use
structure to create numeric ID and points (max 100) as 2
separate members of the structure. Randomly generate
data for N students. Display both the ID and the points of
the student who has received highest point. Write three
separate functions to complete the program:
1. generateStudentData(), input is array of students
2. printStudentInfo(), input is a single student
3. getTopStudent(), input is array of students and output is a
single student
15