Programming for Engineers Bit Manipulation ICEN 200 Spring 2018 - - PowerPoint PPT Presentation

programming for engineers bit manipulation
SMART_READER_LITE
LIVE PREVIEW

Programming for Engineers Bit Manipulation ICEN 200 Spring 2018 - - PowerPoint PPT Presentation

Programming for Engineers Bit Manipulation ICEN 200 Spring 2018 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

Programming for Engineers Bit Manipulation

ICEN 200– Spring 2018

  • 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

Bit Order

Ø Most Significant Bit (MSB) Ø Least Significant Bit (LSB)

01001001

MSB LSB

slide-7
SLIDE 7

7

Field Extraction: Mask

Ø ANDing a bit with 0 produces 0. Ø ANDing a bit with 1 produces the original bit.

01001101 01001101

Data Only rightmost two bits needed

00000011

Mask

01001101

Data

00000001

Result & =

slide-8
SLIDE 8

8

Field Extraction: Mask and Shift

01001101

2nd & 3rd bits needed

01001001

Data

01001001

Data

00001100

Mask

00001000

Result & =

00000010

>> 2

slide-9
SLIDE 9

9

Field Insertion

01 10 00001001

b4b5 b6b7 Data Flag1 Flag2

00000001

Flag1

00000011

Mask

00000001

Shifted & =

01000000

<< 6 Make sure that you have

  • nly two bits in Flag1

00001001

Data | =

01001001

slide-10
SLIDE 10

10

Flip bits

01000101

Flip 2nd & 3rd bits

01001001

Data

01001001

Data

00001100

Mask

01000101

^ =

slide-11
SLIDE 11

11

Display Bits Example (1)

slide-12
SLIDE 12

12

Display Bits Example (2)

slide-13
SLIDE 13

13

Bitwise Operation Example Code (1)

slide-14
SLIDE 14

14

Bitwise Operation Example Code (2)

slide-15
SLIDE 15

15

Bitwise Operation Example Code (3)

slide-16
SLIDE 16

16

Bitwise Operation Example Code (4)

slide-17
SLIDE 17

17

Bitwise Operation Example Code Output

slide-18
SLIDE 18

18

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-19
SLIDE 19

19

Multiply and Divide by Bitwise Operation

Ø Left Shift § Multiply Ø Right Shift (without rotate) § Divide

positional powers of 2: 24 23 22 21 20 decimal positional value: 16 8 4 2 1 binary number: 1 1 1

4 + 2 + 1 = 710

Left shift: 1 1 1 8 + 4 + 2 = 1410

slide-20
SLIDE 20

20

Multiplication using shift

Ø

x * 10

§ x * 10 = x * (8 + 2) = (x * 8) + (x * 2) = (x * 23) + (x * 21) = (x << 3) + (x << 1)

Ø

x * 20

§ x * 20 = x * (16 + 4) = (x * 16) + (x * 4) = (x * 24) + (x * 22) = (x << 4) + (x << 2)

Ø

x * 15

§ x * 15 = x * (16 - 1) = (x * 16) - x = (x * 24) - x = (x << 4) - x