C Programming for Engineers Bit Manipulation ICEN 360 Spring 2017 - - PowerPoint PPT Presentation

c programming for engineers bit manipulation
SMART_READER_LITE
LIVE PREVIEW

C Programming for Engineers Bit Manipulation ICEN 360 Spring 2017 - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

1

C Programming for Engineers Bit Manipulation

ICEN 360– Spring 2017

  • Prof. Dola Saha
slide-2
SLIDE 2

2

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

  • perators.

Ø Bitwise manipulations are machine dependent.

slide-3
SLIDE 3

3

Bitwise Operator

slide-4
SLIDE 4

4

Bitwise Operation Example

00001101 11110010 00011010 n=13 ~n n << 1

Lost 0 0 Inserted

01101000 n << 3

Lost three 0s Inserted three 0’s

00000001 n >> 3

Inserted three 0s 101 Lost

00001101 00001101 00001101

slide-5
SLIDE 5

5

Bitwise Operation Example

00000101 01011101 01011000 00001101 01010101 00001101 01010101 00001101 01010101

n m n&m n m n|m n m n^m

slide-6
SLIDE 6

6

Display Bits Example (1)

slide-7
SLIDE 7

7

Display Bits Example (2)

slide-8
SLIDE 8

8

Bitwise Operation Example Code (1)

slide-9
SLIDE 9

9

Bitwise Operation Example Code (2)

slide-10
SLIDE 10

10

Bitwise Operation Example Code (3)

slide-11
SLIDE 11

11

Bitwise Operation Example Code (4)

slide-12
SLIDE 12

12

Bitwise Operation Example Code Output

slide-13
SLIDE 13

13

Bitwise Operation Application:

source port # dest port #

32 bits

application data (variable length) sequence number acknowledgement number

receive window Urg data pointer checksum

F S R P A U

head Len (4) not used

  • ptions (variable length)

URG: urgent data (generally not used) ACK: ACK # valid PSH: push data now (generally not used) RST, SYN, FIN: connection estab (setup, teardown commands) # bytes rcvr willing to accept counting by bytes

  • f data

(not segments!) Internet checksum (as in UDP)

TCP segment structure

slide-14
SLIDE 14

14

Multiply and Divide by Bitwise Operation

Ø Left Shift § Multiply Ø Right Shift § Divide

slide-15
SLIDE 15

15

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