CS 241: Systems Programming Lecture 13. Bits and Bytes 2 Fall 2019 - - PowerPoint PPT Presentation

cs 241 systems programming lecture 13 bits and bytes 2
SMART_READER_LITE
LIVE PREVIEW

CS 241: Systems Programming Lecture 13. Bits and Bytes 2 Fall 2019 - - PowerPoint PPT Presentation

CS 241: Systems Programming Lecture 13. Bits and Bytes 2 Fall 2019 Prof. Stephen Checkoway 1 Internal data representation Data are stored in binary 32 bit unsigned integer values are: 2 Internal data representation Data are stored in binary


slide-1
SLIDE 1

CS 241: Systems Programming Lecture 13. Bits and Bytes 2

Fall 2019

  • Prof. Stephen Checkoway

1

slide-2
SLIDE 2

Internal data representation

Data are stored in binary 32 bit unsigned integer values are:

2

slide-3
SLIDE 3

Internal data representation

Data are stored in binary 32 bit unsigned integer values are: 00000000 00000000 00000000 00000000 = 0

2

slide-4
SLIDE 4

Internal data representation

Data are stored in binary 32 bit unsigned integer values are: 00000000 00000000 00000000 00000000 = 0 00000000 00000000 00000000 00000001 = 1

2

slide-5
SLIDE 5

Internal data representation

Data are stored in binary 32 bit unsigned integer values are: 00000000 00000000 00000000 00000000 = 0 00000000 00000000 00000000 00000001 = 1 00000000 00000000 00000000 00000010 = 2

2

slide-6
SLIDE 6

Internal data representation

Data are stored in binary 32 bit unsigned integer values are: 00000000 00000000 00000000 00000000 = 0 00000000 00000000 00000000 00000001 = 1 00000000 00000000 00000000 00000010 = 2 00000000 00000000 00000000 00000011 = 3

2

slide-7
SLIDE 7

Internal data representation

Data are stored in binary 32 bit unsigned integer values are: 00000000 00000000 00000000 00000000 = 0 00000000 00000000 00000000 00000001 = 1 00000000 00000000 00000000 00000010 = 2 00000000 00000000 00000000 00000011 = 3 … 11111111 11111111 11111111 11111111 = 232-1

2

slide-8
SLIDE 8

Bitwise operators

Binary operators apply the operation to the corresponding bits of the

  • perands
  • x & y — bitwise AND
  • x | y — bitwise OR
  • x ^ Y — bitwise XOR

Unary operator applies the operation to each bit

  • ~x

— one's complement (flip each bit)

3

slide-9
SLIDE 9

Review of Boolean logic

4

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

slide-10
SLIDE 10

What is the value of 0x4E & 0x1F?

  • A. 0xE
  • B. 0x51
  • C. 0x5F
  • D. 0xB1
  • E. 0xE0

5

Hex Binary Hex Binary 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111

slide-11
SLIDE 11

Bit shifting

6

slide-12
SLIDE 12

Bit shifting

Manipulates the position of bits

6

slide-13
SLIDE 13

Bit shifting

Manipulates the position of bits

  • Left shift fills with 0 bits

6

slide-14
SLIDE 14

Bit shifting

Manipulates the position of bits

  • Left shift fills with 0 bits
  • Right shift of unsigned variable fills with 0 bits

6

slide-15
SLIDE 15

Bit shifting

Manipulates the position of bits

  • Left shift fills with 0 bits
  • Right shift of unsigned variable fills with 0 bits
  • Right shift of signed variable fills with sign bit (Actually implementation

defined if negative!)

6

slide-16
SLIDE 16

Bit shifting

Manipulates the position of bits

  • Left shift fills with 0 bits
  • Right shift of unsigned variable fills with 0 bits
  • Right shift of signed variable fills with sign bit (Actually implementation

defined if negative!) x << 2; // shifts bits of x two positions left

6

slide-17
SLIDE 17

Bit shifting

Manipulates the position of bits

  • Left shift fills with 0 bits
  • Right shift of unsigned variable fills with 0 bits
  • Right shift of signed variable fills with sign bit (Actually implementation

defined if negative!) x << 2; // shifts bits of x two positions left

  • Same as multiplying by 4

6

slide-18
SLIDE 18

Bit shifting

Manipulates the position of bits

  • Left shift fills with 0 bits
  • Right shift of unsigned variable fills with 0 bits
  • Right shift of signed variable fills with sign bit (Actually implementation

defined if negative!) x << 2; // shifts bits of x two positions left

  • Same as multiplying by 4

x >> 3; // shifts bits of x three positions right

6

slide-19
SLIDE 19

Bit shifting

Manipulates the position of bits

  • Left shift fills with 0 bits
  • Right shift of unsigned variable fills with 0 bits
  • Right shift of signed variable fills with sign bit (Actually implementation

defined if negative!) x << 2; // shifts bits of x two positions left

  • Same as multiplying by 4

x >> 3; // shifts bits of x three positions right

  • Same as dividing by 8 (if x is unsigned)

6

slide-20
SLIDE 20

What does the following do?

  • A. Changes x to be positive
  • B. Sets the least significant two bits to 0
  • C. Sets the most significant two bits to 0
  • D. Gives an integer overflow error
  • E. Implementation-defined behavior

7

x = ((x >> 2) << 2);

slide-21
SLIDE 21

Testing if a bit is set (i.e., is 1)

8

#include <stdbool.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); // 1u is an unsigned int with value 1. }

slide-22
SLIDE 22

Testing if a bit is set (i.e., is 1)

1u << n gives an integer with only the nth bit set

8

#include <stdbool.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); // 1u is an unsigned int with value 1. }

slide-23
SLIDE 23

Testing if a bit is set (i.e., is 1)

1u << n gives an integer with only the nth bit set If the nth bit is 1, then x & (1u << n) is 1u << n which is nonzero.
 If the nth bit is 0, then x & (1u << n) is 0

8

#include <stdbool.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); // 1u is an unsigned int with value 1. }

slide-24
SLIDE 24

Testing if a bit is set (i.e., is 1)

1u << n gives an integer with only the nth bit set If the nth bit is 1, then x & (1u << n) is 1u << n which is nonzero.
 If the nth bit is 0, then x & (1u << n) is 0 What happens if n is too large?

8

#include <stdbool.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); // 1u is an unsigned int with value 1. }

slide-25
SLIDE 25

Testing if a bit is set (i.e., is 1)

1u << n gives an integer with only the nth bit set If the nth bit is 1, then x & (1u << n) is 1u << n which is nonzero.
 If the nth bit is 0, then x & (1u << n) is 0 What happens if n is too large?

  • Undefined behavior!

8

#include <stdbool.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); // 1u is an unsigned int with value 1. }

slide-26
SLIDE 26

UB

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-27
SLIDE 27

UB

$ ./bad_shift 3 0

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-28
SLIDE 28

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-29
SLIDE 29

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1 $ ./bad_shift 3 1

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-30
SLIDE 30

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1 $ ./bad_shift 3 1 Bit 1 of 3 is 1

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-31
SLIDE 31

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1 $ ./bad_shift 3 1 Bit 1 of 3 is 1 $ ./bad_shift 3 2

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-32
SLIDE 32

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1 $ ./bad_shift 3 1 Bit 1 of 3 is 1 $ ./bad_shift 3 2 Bit 2 of 3 is 0

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-33
SLIDE 33

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1 $ ./bad_shift 3 1 Bit 1 of 3 is 1 $ ./bad_shift 3 2 Bit 2 of 3 is 0 $ ./bad_shift 3 32

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-34
SLIDE 34

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1 $ ./bad_shift 3 1 Bit 1 of 3 is 1 $ ./bad_shift 3 2 Bit 2 of 3 is 0 $ ./bad_shift 3 32 Bit 32 of 3 is 1

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-35
SLIDE 35

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1 $ ./bad_shift 3 1 Bit 1 of 3 is 1 $ ./bad_shift 3 2 Bit 2 of 3 is 0 $ ./bad_shift 3 32 Bit 32 of 3 is 1 $ ./bad_shift 3 33

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-36
SLIDE 36

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1 $ ./bad_shift 3 1 Bit 1 of 3 is 1 $ ./bad_shift 3 2 Bit 2 of 3 is 0 $ ./bad_shift 3 32 Bit 32 of 3 is 1 $ ./bad_shift 3 33 Bit 33 of 3 is 1

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-37
SLIDE 37

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1 $ ./bad_shift 3 1 Bit 1 of 3 is 1 $ ./bad_shift 3 2 Bit 2 of 3 is 0 $ ./bad_shift 3 32 Bit 32 of 3 is 1 $ ./bad_shift 3 33 Bit 33 of 3 is 1 $ ./bad_shift 3 34

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-38
SLIDE 38

UB

$ ./bad_shift 3 0 Bit 0 of 3 is 1 $ ./bad_shift 3 1 Bit 1 of 3 is 1 $ ./bad_shift 3 2 Bit 2 of 3 is 0 $ ./bad_shift 3 32 Bit 32 of 3 is 1 $ ./bad_shift 3 33 Bit 33 of 3 is 1 $ ./bad_shift 3 34 Bit 34 of 3 is 0

9

#include <err.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { return x & (1u << n); } int main(int argc, char **argv) { if (argc != 3) errx(1, "Usage: %s integer bit", argv[0]); unsigned int x = atoi(argv[1]); unsigned int n = atoi(argv[2]); if (is_bit_set(x, n)) printf("Bit %u of %u is 1\n", n, x); else printf("Bit %u of %u is 0\n", n, x); return 0; }

slide-39
SLIDE 39

Testing if a bit is set (i.e., is 1)

10

#include <assert.h> #include <limits.h> #include <stdbool.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { // assert(cond) will abort at runtime if cond is false. assert(n < CHAR_BIT * sizeof x); return x & (1u << n); // 1u is an unsigned int with value 1. }

slide-40
SLIDE 40

Testing if a bit is set (i.e., is 1)

10

#include <assert.h> #include <limits.h> #include <stdbool.h> // Returns true if the nth bit of x is 1. bool is_bit_set(unsigned int x, unsigned int n) { // assert(cond) will abort at runtime if cond is false. assert(n < CHAR_BIT * sizeof x); return x & (1u << n); // 1u is an unsigned int with value 1. } E.g., if CHAR_BIT is 8 and sizeof x is 4, then n must be less than 32 or the program aborts

slide-41
SLIDE 41

Setting a bit (to 1)

11

// Returns the value of x with the nth bit set to 1. unsigned int set_bit(unsigned int x, unsigned int n) { assert(n < CHAR_BIT * sizeof x); return x | (1u << n); }

slide-42
SLIDE 42

Clearing a bit (setting it to 0)

12

// Returns the value of x with the nth bit set to 0. unsigned int set_bit(unsigned int x, unsigned int n) { assert(n < CHAR_BIT * sizeof x); return x & ~(1u << n); }

slide-43
SLIDE 43

Clearing a bit (setting it to 0)

12

// Returns the value of x with the nth bit set to 0. unsigned int set_bit(unsigned int x, unsigned int n) { assert(n < CHAR_BIT * sizeof x); return x & ~(1u << n); } 1u << n gives an integer with just the nth bit set

slide-44
SLIDE 44

Clearing a bit (setting it to 0)

12

// Returns the value of x with the nth bit set to 0. unsigned int set_bit(unsigned int x, unsigned int n) { assert(n < CHAR_BIT * sizeof x); return x & ~(1u << n); } 1u << n gives an integer with just the nth bit set ~(1u << n) gives an integer with all bits set except the nth bit

slide-45
SLIDE 45

Given an unsigned integer x with some value, what value should we use for mask to clear all of the bits of x except for the least significant 5 bits? unsigned int x = /* … */; // Given some value here,
 unsigned int mask = /* … */; // what value goes here
 x = x & mask; // to clear the required bits?

  • A. 0x5u
  • B. ~0x5u
  • C. 0x1Fu
  • D. ~0x1Fu
  • E. sizeof x - 5

13

slide-46
SLIDE 46

Given an unsigned integer x with some value, what value should we use for mask to clear the 5 least significant bits of x? unsigned int x = /* … */; // Given some value here,
 unsigned int mask = /* … */; // what value goes here
 x = x & mask; // to clear the required bits?

  • A. 0x5u
  • B. ~0x5u
  • C. 0x1Fu
  • D. ~0x1Fu
  • E. sizeof x - 5

14

slide-47
SLIDE 47

Combining flags via |

Specify flags via individual bits Combine flags with | E.g., set file system permissions via the flags
 S_I{R,W,X}{USR,GRP,OTH}

15

#define S_IRWXU 0000700 /* RWX mask for owner */ #define S_IRUSR 0000400 /* R for owner */ #define S_IWUSR 0000200 /* W for owner */ #define S_IXUSR 0000100 /* X for owner */ #define S_IRWXG 0000070 /* RWX mask for group */ #define S_IRGRP 0000040 /* R for group */ #define S_IWGRP 0000020 /* W for group */ #define S_IXGRP 0000010 /* X for group */ #define S_IRWXO 0000007 /* RWX mask for other */ #define S_IROTH 0000004 /* R for other */ #define S_IWOTH 0000002 /* W for other */ #define S_IXOTH 0000001 /* X for other */ int chmod(char const *path, mode_t mode);

slide-48
SLIDE 48

Negative numbers

16

slide-49
SLIDE 49

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

16

slide-50
SLIDE 50

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits

slide-51
SLIDE 51

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101

slide-52
SLIDE 52

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010

slide-53
SLIDE 53

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010 Add 1: 1111_1011

slide-54
SLIDE 54

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

Computing -x from x (regardless of sign)

  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010 Add 1: 1111_1011

slide-55
SLIDE 55

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

Computing -x from x (regardless of sign)

  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010 Add 1: 1111_1011

  • (-5) in 8 bits
slide-56
SLIDE 56

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

Computing -x from x (regardless of sign)

  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010 Add 1: 1111_1011

  • (-5) in 8 bits
  • 5: 1111_1011
slide-57
SLIDE 57

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

Computing -x from x (regardless of sign)

  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010 Add 1: 1111_1011

  • (-5) in 8 bits
  • 5: 1111_1011

invert bits: 0000_0100

slide-58
SLIDE 58

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

Computing -x from x (regardless of sign)

  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010 Add 1: 1111_1011

  • (-5) in 8 bits
  • 5: 1111_1011

invert bits: 0000_0100 Add 1: 0000_0101

slide-59
SLIDE 59

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

Computing -x from x (regardless of sign)

  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010 Add 1: 1111_1011

  • (-5) in 8 bits
  • 5: 1111_1011

invert bits: 0000_0100 Add 1: 0000_0101 0: 0000_0000

slide-60
SLIDE 60

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

Computing -x from x (regardless of sign)

  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010 Add 1: 1111_1011

  • (-5) in 8 bits
  • 5: 1111_1011

invert bits: 0000_0100 Add 1: 0000_0101 0: 0000_0000 invert bits: 1111_1111

slide-61
SLIDE 61

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

Computing -x from x (regardless of sign)

  • Invert all of the bits
  • Add 1

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010 Add 1: 1111_1011

  • (-5) in 8 bits
  • 5: 1111_1011

invert bits: 0000_0100 Add 1: 0000_0101 0: 0000_0000 invert bits: 1111_1111 Add 1: 1_0000_0000

slide-62
SLIDE 62

Negative numbers

Usually stored using two's complement

  • Take the magnitude of the number
  • Invert all of the bits
  • Add 1

Computing -x from x (regardless of sign)

  • Invert all of the bits
  • Add 1

Most significant bit indicates the sign

  • 1 indicates a negative number

16

representation of -5 in 8 bits magnitude: 0000_0101 invert bits: 1111_1010 Add 1: 1111_1011

  • (-5) in 8 bits
  • 5: 1111_1011

invert bits: 0000_0100 Add 1: 0000_0101 0: 0000_0000 invert bits: 1111_1111 Add 1: 1_0000_0000

slide-63
SLIDE 63

Signed numbers in two's complement

10000000 00000000 00000000 00000000 = -231 10000000 00000000 00000000 00000001 = -231+1 … 11111111 11111111 11111111 11111110 = -2 11111111 11111111 11111111 11111111 = -1 00000000 00000000 00000000 00000000 = 0 00000000 00000000 00000000 00000001 = 1 00000000 00000000 00000000 00000010 = 2 00000000 00000000 00000000 00000011 = 3 … 01111111 11111111 11111111 11111110 = 231-2 01111111 11111111 11111111 11111111 = 231-1

17

slide-64
SLIDE 64

Not the only choice

18

slide-65
SLIDE 65

Not the only choice

Sign and magnitude

  • Most significant bit represents the sign, remaining bits are the magnitude
  • Range -(2n-1 - 1) to 2n-1 - 1
  • Two different bit patterns for zero: 0 and 0x80000000 (assuming 32-bits)

18

slide-66
SLIDE 66

Not the only choice

Sign and magnitude

  • Most significant bit represents the sign, remaining bits are the magnitude
  • Range -(2n-1 - 1) to 2n-1 - 1
  • Two different bit patterns for zero: 0 and 0x80000000 (assuming 32-bits)

Ones' complement

  • Negative numbers are the bitwise inverse of positive numbers (-x = ~x)
  • Range -(2n-1 - 1) to 2n-1 - 1
  • Two different bit patterns for zero: 0 and 0xFFFFFFFF (assuming 32-bits)

18

slide-67
SLIDE 67

Not the only choice

Sign and magnitude

  • Most significant bit represents the sign, remaining bits are the magnitude
  • Range -(2n-1 - 1) to 2n-1 - 1
  • Two different bit patterns for zero: 0 and 0x80000000 (assuming 32-bits)

Ones' complement

  • Negative numbers are the bitwise inverse of positive numbers (-x = ~x)
  • Range -(2n-1 - 1) to 2n-1 - 1
  • Two different bit patterns for zero: 0 and 0xFFFFFFFF (assuming 32-bits)

Two's complement

  • Negative numbers are ones' complement plus one (-x = ~x + 1)
  • Range -2n-1 to 2n-1 - 1
  • Only one zero

18

slide-68
SLIDE 68

In-class exercise

https://checkoway.net/teaching/cs241/2019-fall/exercises/Lecture-13.html Grab a laptop and a partner and try to get as much of that done as you can!

19