0% found this document useful (0 votes)
175 views15 pages

Bit Manipulation

This document summarizes bitwise operations in C programming. It discusses how computers represent data internally as bit sequences and how bitwise operators can manipulate the bits of integer operands. Examples are given to demonstrate left and right shifting, AND, OR, and XOR operations on bits. Code snippets show how to display individual bits and apply bitwise operations. Applications for multiplying and dividing using bitwise operations and the TCP segment structure are also mentioned. The document concludes with an example classwork assignment on generating student data using structures and functions.

Uploaded by

vkrfmpnfty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views15 pages

Bit Manipulation

This document summarizes bitwise operations in C programming. It discusses how computers represent data internally as bit sequences and how bitwise operators can manipulate the bits of integer operands. Examples are given to demonstrate left and right shifting, AND, OR, and XOR operations on bits. Code snippets show how to display individual bits and apply bitwise operations. Applications for multiplying and dividing using bitwise operations and the TCP segment structure are also mentioned. The document concludes with an example classwork assignment on generating student data using structures and functions.

Uploaded by

vkrfmpnfty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

C Programming for

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

n << 3 Lost 00001101


Inserted
three 0s 01101000 three 0’s

n >> 3 Inserted 00001101


three 0s 00000001 101 Lost

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

You might also like