CSC 2400: Computer Systems Bit-Level vs. Logical Operators Review - - PowerPoint PPT Presentation

csc 2400 computer systems
SMART_READER_LITE
LIVE PREVIEW

CSC 2400: Computer Systems Bit-Level vs. Logical Operators Review - - PowerPoint PPT Presentation

CSC 2400: Computer Systems Bit-Level vs. Logical Operators Review Addition q 2s complement addition is just binary addition - assume all integers have the same number of bits - for now, assume no overflow 01101000 (104) 11110110 (-10) +


slide-1
SLIDE 1

CSC 2400: Computer Systems

Bit-Level vs. Logical Operators

slide-2
SLIDE 2

Review – Addition

q 2’s complement addition is just binary addition

  • assume all integers have the same number of bits
  • for now, assume no overflow

01101000 (104) 11110110

(-10)

+ 11110000 (-16) +

(-9)

01011000

(88) (-19) Assume 8-bit 2’s complement numbers.

slide-3
SLIDE 3

Review – Sign Extension

q To add two numbers, we must represent them with the

same number of bits.

q If we just pad with zeroes on the left NOT GOOD!): q Instead, replicate the sign bit to the left:

4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 00001100 (12, not -4; NOT GOOD!) 4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 11111100 (still -4)

slide-4
SLIDE 4

Review – Memory and Variables

int i = 258; 00000000 00000000 00000001 00000010 int i = 0x102; 00000000 00000000 00000001 00000010 01000001 char c = ‘A’; 01000001 char c = 65; 01000001 char c = 0x41; int j = 0xF0A2; 00000000 00000000 11110000 10010010

slide-5
SLIDE 5

Bit-Level Operators

Common to C and Java &, |, ~, ^, <<, >>

slide-6
SLIDE 6

Bitwise Operators

~ Bitwise NOT “flips” bits & Bitwise AND 1 if both operands are 1 | Bitwise OR 0 if both operands are 0 ^ Bitwise XOR 0 if operands are equal << Bitwise Left Shift shifts bits to the left >> Bitwise Right Shift shifts bits to the right

  • Apply to any “integral” data type: long, int, short, char
  • View arguments as bit vectors
  • Arguments applied bit-wise
slide-7
SLIDE 7

Bitwise Operators

A B ~A A&B A|B A^B A^A 1 1 1 1

slide-8
SLIDE 8

NOT (~) Example

Input 1 1 1 1 0xB4 ~Input 0x___

slide-9
SLIDE 9

AND (&) and OR (I) and XOR (^) Examples

A 1 1 1 1 0xB4 B 1 1 1 1 1 0x___ A & B 0x___ A | B 0x___ A ^ B 0x___ A ^ A 0x___

slide-10
SLIDE 10

AND (&) and OR (I) and XOR (^) Examples

0x69 & 0x55 --> 0x______ 0x69 | 0x55 --> 0x______ 0x69 ^ 0x55 --> 0x______

slide-11
SLIDE 11

q Used to change or query one or more bits in a variable q The bitmask indicates which bits are to be affected q Common operations:

  • Setting one or more bits to 1
  • Setting one or more bits to 0
  • Reading one or more bits

Bitmasks

slide-12
SLIDE 12

q Assume that data is an integer variable (size is unknown) q Set the least significant bit of data to 1 (other bits are unchanged)

Bitmasks – set single bit to 1

data 1 1 1 1 ? mask result wanted 1 1 1 1 1 data = _______________________;

q C statement:

data | 1

slide-13
SLIDE 13

q Assume that data is an integer variable (size is unknown) q Set bit 0, 2 and 3 of data to 1 (leave the other bits unchanged):

Bitmasks – set multiple bits to 1

data 1 1 1 ? ? ? mask result wanted 1 1 1 1 1 1 data = _______________________;

q C statement:

data | 0xD

slide-14
SLIDE 14

q Assume that data is an integer variable (size is unknown) q Set the least significant byte of data to 0xFF

Bitmasks – set byte to 0xFF

data 1 1 1 1 ? ? ? ? ? ? ? ? mask result wanted 1 1 1 1 1 1 1 1 1 1 1 1 data = _______________________;

q C statement:

data | 0xFF

slide-15
SLIDE 15

q Assume that data is an integer variable (size is unknown) q Set the 2nd least significant byte of data to 0xFF

Bitmasks – set another byte to 0xFF

data ? ? ? ? ? ? ? ? 1 1 1 1 mask result wanted 1 1 1 1 1 1 1 1 1 1 1 1 data = _______________________;

q C statement:

data | 0xFF00

slide-16
SLIDE 16

q Assume that data is an integer variable (size is unknown) q Set the least significant bit of data to 0:

Bitmasks – clear (set to 0) single bit

data 1 1 1 1 1 ? mask result wanted 1 1 1 1 1 data = _______________________;

q C statement:

data & ~1

slide-17
SLIDE 17

q Set one or more bits to 1 q Set one or more bits to 0 q Read one or more bits

Review – Common Bit Operations

slide-18
SLIDE 18

Review – What Does This Code Do?

int data = ...; data = data | 0x1;

data ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | 0x1 1 result ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1

q Answer:

  • Set the least significant bit to 1
slide-19
SLIDE 19

Review – What Does This Code Do?

int data = ...; data = data | 0x4;

data ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | 0x4 1 result ? ? ? ? ? ? ? ? ? ? ? ? ? 1 ? ?

q Answer:

  • Set bit 2 (third least significant bit) to 1

bit 0 bit 1 bit 2

slide-20
SLIDE 20

Review – What Does This Code Do?

int data = ...; data = data | 0xF;

data ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | 0xF 1 1 1 1 result ? ? ? ? ? ? ? ? ? ? ? ? 1 1 1 1

q Answer:

  • Set the four least significant bits to 1

bit 0 bit 1 bit 2 bit 3

slide-21
SLIDE 21

Review – What Does This Code Do?

int data = ...; data = data & ~0x1;

data ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? & ~0x1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 result ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

q Answer:

  • Clear the least significant bit (set it to 0)

bit 0

slide-22
SLIDE 22

q Assume that data is an integer variable (size is unknown) q Clear (set to 0) bits 0, 2 and 4 of data (other bits are unchanged):

Bitmasks – clear (set to 0) multiple bits

data 1 1 ? ? 1 ? mask result wanted 1 1 1 data = _______________________;

q C statement:

data & ~0x15

slide-23
SLIDE 23

q Assume that data is an integer variable (size is unknown) q Clear (set to 0) the least significant byte of data

Bitmasks – clear (set to 0) single byte

data 1 1 1 1 ? ? ? ? ? ? ? ? mask result wanted 1 1 1 1 data = _______________________;

q C statement:

data & ~0xFF

slide-24
SLIDE 24

Feedback Results

q Review Topics (Today) q Ungraded Practice Quizzes (1/week) q Review Homework q More Hands-on/Labs/C Programming (Coming Up) q Teaching

  • Slow Down
  • Explain Jargon
  • Step-by-step Examples

q When post slides?

slide-25
SLIDE 25

q Assume that data is an integer variable (size is unknown) q Clear (set to 0) bytes 0 and 1 of data

Bitmasks – clear (set to 0) multiple bytes

data ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mask result wanted data = _______________________;

q C statement:

data & ~0xFFFF

slide-26
SLIDE 26

q Assume that data is an integer variable (size is unknown) q Determine the least significant bit of data:

Bitmasks – read bit

data 1 1 1 1 ? mask result wanted ? result = _______________________;

q C statement:

data & 1

slide-27
SLIDE 27

q Assume that data is an integer variable (size is unknown) q Determine the 2nd least significant bit of data:

Bitmasks – read another bit

data 1 1 1 1 ? 1 mask result wanted ?

q Need to shift

slide-28
SLIDE 28

Bitwise Operator: Left Shift <<

q Left shift: A << n (same as multiply by 2n)

  • Shift the bit-vector A to the left by n bits
  • Leftmost n bits are lost; rightmost n bits become 0

A 1 1 1 1 0xB4 A << 3 1 1 0x___

slide-29
SLIDE 29

Bitwise Operator: Right Shift >>

q Right shift: A >> n (same as divide by 2n)

  • Shift the bit-vector A to the right by n bits
  • Rightmost n bits are lost; what about leftmost n bits?

char A; 1 1 1 0x34 A >> 3 1 1 0x___ char A; 1 1 1 1 0xB4 A >> 3 1 1 1 0x___

1 1 1 0 0 0

q Uses sign extension for signed types

slide-30
SLIDE 30

Bitwise Operator: Right Shift >>

unsigned char A; 1 1 1 0x34 A >> 3 1 1 0x___ unsigned char A; 1 1 1 1 0xB4 A >> 3 1 1 1 0x___

0 0 0 0 0 0

Note: Java has the unsigned shift operator >>> (not available in C)

q Insert 0 to the left for unsigned types

slide-31
SLIDE 31

q Determine the 2nd least significant bit of data (size unknown):

Bitmasks – revisit example

data 1 1 1 1 ? 1 data >> 1 ? mask result wanted ? result = _______________________;

q C statement:

(data >> 1) & 1

slide-32
SLIDE 32

q Determine the 4th least significant bit of data (size unknown):

Bitmasks – read another bit

data 1 1 ? 1 1 result ? result = _______________________;

q C statement:

(data >> 4) & 1

slide-33
SLIDE 33

q Assume that data is an integer variable (size is unknown) q Determine the 2nd least significant byte of data

Bitmasks – read byte

data ? ? ? ? ? ? ? ? 1 1 1 1 1 result ? ? ? ? ? ? ? ? result = _______________________;

q C statement:

(data >> 8) & 0xFF

slide-34
SLIDE 34

char c = 0xB5; void print_binary(char c) { /* add code here */ }

Bitwise Operators

q Write code to print all the bits of a character from left

to right (starting with the most significant bit):

1 0 1 1 0 1 0 1

slide-35
SLIDE 35

Relations Between Operations

q DeMorgan’s Laws

  • Express & in terms of |, and vice-versa
  • A & B = ~(~A | ~B)
  • A and B are true if and only if neither A nor B is false
  • A | B = ~(~A & ~B)
  • A or B are true if and only if A and B are not both false

q Exclusive-Or using Inclusive Or

  • A ^ B = (~A & B) | (A & ~B)
  • Exactly one of A and B is true
  • A ^ B = (A | B) & ~(A & B)
  • Either A is true, or B is true, but not both
slide-36
SLIDE 36

Why Bitwise Operators?

slide-37
SLIDE 37

Used in Networking

slide-38
SLIDE 38

Used in Encryption/ Decryption

slide-39
SLIDE 39

Used in Compression

  • Look at the DEFLATE algorithm, for instance
  • https://en.wikipedia.org/wiki/DEFLATE
  • everything is bits, not bytes
slide-40
SLIDE 40

~ & | ^ << >>

Review – Bitwise Operators

slide-41
SLIDE 41

Contrast: Logical Operators

slide-42
SLIDE 42

Logical Operators

q Always return 0 or 1 q False is 0 in C q True is anything nonzero in C q Examples (char data type)

  • !0x41 --> _____
  • !0x00 --> _____
  • !!0x41 --> _____
  • 0x69 && 0x55 --> _____
  • 0x69 || 0x55 --> _____

&& (Logical AND), || (Logical OR), ! (Logical NOT)

slide-43
SLIDE 43

What is the Output?

int a = 0x43, b = 0x21; printf("a | b = %x\n", a | b); _________ Printf("a || b = %x\n", a || b); _________ printf("a & b = %x\n", a & b); _________ printf("a && b = %x\n", a && b); _________

slide-44
SLIDE 44

Mental Exercise

/* * isNotEqual - return 0 if x == y, and 1 otherwise * Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1 */ int isNotEqual (int x, int y) { return ______________; }

slide-45
SLIDE 45

What did we learn?

q Bit-level operators (&, |, ~, ^, <<, >>)

  • Operate on bits (view arguments as bit vectors)

q Logical operators (&&, ||, !)

  • Always return 0 (False) or 1 (True)
slide-46
SLIDE 46

Midterm Review Topics

q Number Representations and Casting/Conversion:

  • Binary, Hexadecimal, Octal, Little and big endian, sign extension
  • Signed Magnitude, 1’s Complement, 2’s Complement
  • IEEE 32-bit and 64-bit Floating Point, ASCII
  • Addition in different representations, overflow, limits of representation

q Bitwise and Logical Operators: AND, OR, NOT, XOR, L&R SHIFT q Unix Commands: ls, cd, mkdir, cp, mv, rm, chmod, pwd, -r, etc

  • Absolute and Relative Paths, permissions, navigating directory structure

q C programming and differences with Java q Computer Systems (hw/sw, analog/digital), Data Sizes, etc q Sources: ZyBook, Tarnoff, Slides, Labs and Homework q Problems: Short Answer, MC, True False, and Problem Solving