• No results found

CS107 Lecture 2 Bits and Bytes; Integer Representations

N/A
N/A
Protected

Academic year: 2021

Share "CS107 Lecture 2 Bits and Bytes; Integer Representations"

Copied!
137
0
0

Loading.... (view fulltext now)

Full text

(1)

CS107 Lecture 2

Bits and Bytes; Integer Representations

reading:

Bryant & O’Hallaron, Ch. 2.2-2.3

(2)

CS107 Topic 1: How can a computer represent integer

numbers?

(3)

Demo: Unexpected

Behavior

(4)

Lecture Plan

• Bits and Bytes 5

• Hexadecimal 33

• Integer Representations 41

• Unsigned Integers 47

• Signed Integers 51

• Overflow 77

• Casting and Combining Types 90

• Live Session 119

(5)

Lecture Plan

Bits and Bytes 5

• Hexadecimal 33

• Integer Representations 41

• Unsigned Integers 47

• Signed Integers 51

• Overflow 77

• Casting and Combining Types 90

• Live Session 119

(6)

0

(7)

1

(8)

Bits

• Computers are built around the idea of two states: “on” and “off”. Transistors represent this in hardware, and bits represent this in software!

(9)

One Bit At A Time

We can combine bits, like with base-10 numbers, to represent more data. 8 bits = 1 byte.

Computer memory is just a large array of bytes! It is byte-addressable; you can’t address (store location of) a bit; only a byte.

• Computers still fundamentally operate on bits; we have just gotten more creative about how to represent different data as bits!

Images

Audio

Video

Text

And more…

(10)

Base 10

5 9 3 4

Digits 0-9 (0 to base-1)

(11)

Base 10

5 9 3 4

ones tens

hundr

eds thousands

= 5*1000 + 9*100 + 3*10 + 4*1

(12)

Base 10

5 9 3 4

100 101

102 103

(13)

Base 10

5 9 3 4

0 1

2 3

10X:

(14)

Base 2

1 0 1 1

0 1

2 3

2X:

Digits 0-1 (0 to base-1)

(15)

Base 2

1 0 1 1

20 21

22 23

(16)

Base 2

1 0 1 1

ones twos

fours eights

Most significant bit (MSB) Least significant bit (LSB)

(17)

Base 10 to Base 2

Question: What is 6 in base 2?

• Strategy:

What is the largest power of 2 ≤ 6?

(18)

Base 10 to Base 2

_ _ _ _

Question: What is 6 in base 2?

• Strategy:

What is the largest power of 2 ≤ 6? 22=4

1

0

(19)

Base 10 to Base 2

_ _ _ _

Question: What is 6 in base 2?

• Strategy:

What is the largest power of 2 ≤ 6? 22=4

Now, what is the largest power of 2 ≤ 6 – 22?

20 21

22 23

1

0

(20)

Base 10 to Base 2

_ _ _ _

Question: What is 6 in base 2?

• Strategy:

What is the largest power of 2 ≤ 6? 22=4

Now, what is the largest power of 2 ≤ 6 – 22? 21=2

1

0 1

(21)

Base 10 to Base 2

_ _ _ _

Question: What is 6 in base 2?

• Strategy:

What is the largest power of 2 ≤ 6? 22=4

Now, what is the largest power of 2 ≤ 6 – 22? 21=2

6 – 22 – 21 = 0!

20 21

22 23

1

0 1

(22)

Base 10 to Base 2

_ _ _ _

Question: What is 6 in base 2?

• Strategy:

What is the largest power of 2 ≤ 6? 22=4

Now, what is the largest power of 2 ≤ 6 – 22? 21=2

6 – 22 – 21 = 0!

1

0 1 0

(23)

Base 10 to Base 2

_ _ _ _

Question: What is 6 in base 2?

• Strategy:

What is the largest power of 2 ≤ 6? 22=4

Now, what is the largest power of 2 ≤ 6 – 22? 21=2

6 – 22 – 21 = 0!

20 21

22 23

1

0 1 0

(24)

Practice: Base 2 to Base 10

What is the base-2 value 1010 in base-10?

a) 20 b) 101 c) 10 d) 5

e) Other

(25)

Practice: Base 10 to Base 2

What is the base-10 value 14 in base 2?

a) 1111

b) 1110 c) 1010 d) Other

(26)

Byte Values

• What is the minimum and maximum base-10 value a single byte (8 bits) can store?

(27)

Byte Values

• What is the minimum and maximum base-10 value a single byte (8 bits) can store? minimum = 0 maximum = ?

(28)

Byte Values

• What is the minimum and maximum base-10 value a single byte (8 bits) can store? minimum = 0 maximum = ?

11111111

2x: 7 6 5 4 3 2 1 0

(29)

Byte Values

• What is the minimum and maximum base-10 value a single byte (8 bits) can store? minimum = 0 maximum = ?

Strategy 1: 1*27 + 1*26 + 1*25 + 1*24 + 1*23+ 1*22 + 1*21 + 1*20 = 255

11111111

2x: 7 6 5 4 3 2 1 0

(30)

Byte Values

• What is the minimum and maximum base-10 value a single byte (8 bits) can store? minimum = 0 maximum = 255

11111111

2x: 7 6 5 4 3 2 1 0

(31)

Multiplying by Base

1450 x 10 = 14500 1100 2 x 2 = 11000

Key Idea: inserting 0 at the end multiplies by the base!

(32)

Dividing by Base

1450 / 10 = 145 1100 2 / 2 = 110

Key Idea: removing 0 at the end divides by the base!

(33)

Lecture Plan

• Bits and Bytes 5

Hexadecimal 33

• Integer Representations 41

• Unsigned Integers 47

• Signed Integers 51

• Overflow 77

• Casting and Combining Types 90

• Live Session 119

(34)

Hexadecimal

• When working with bits, oftentimes we have large numbers with 32 or 64 bits.

Instead, we’ll represent bits in base-16 instead; this is called hexadecimal.

0110 1010 0011

0-15 0-15 0-15

(35)

Hexadecimal

• When working with bits, oftentimes we have large numbers with 32 or 64 bits.

Instead, we’ll represent bits in base-16 instead; this is called hexadecimal.

0-15 0-15 0-15

Each is a base-16 digit!

(36)

Hexadecimal

Hexadecimal is base-16, so we need digits for 1-15. How do we do this?

0 1 2 3 4 5 6 7 8 9 a b c d e f

10 11 12 13 14 15

(37)

Hexadecimal

Hex digit 0 1 2 3 4 5 6 7

Decimal value 0 1 2 3 4 5 6 7

Binary value 0000 0001 0010 0011 0100 0101 0110 0111

Hex digit 8 9 A B C D E F

Decimal value 8 9 10 11 12 13 14 15

Binary value 1000 1001 1010 1011 1100 1101 1110 1111

(38)

Hexadecimal

We distinguish hexadecimal numbers by prefixing them with 0x, and binary numbers with 0b.

E.g. 0xf5 is 0b11110101

0x f 5

1111 0101

(39)

Practice: Hexadecimal to Binary

What is 0x173A in binary?

Hexadecimal 1 7 3 A

Binary 0001 0111 0011 1010

(40)

Practice: Hexadecimal to Binary

What is 0b1111001010 in hexadecimal? (Hint: start from the right)

Binary 11 1100 1010

Hexadecimal 3 C A

(41)

Lecture Plan

• Bits and Bytes 5

• Hexadecimal 33

Integer Representations 41

• Unsigned Integers 47

• Signed Integers 51

• Overflow 77

• Casting and Combining Types 90

• Live Session 119

(42)

Number Representations

Unsigned Integers: positive and 0 integers. (e.g. 0, 1, 2, … 99999…

Signed Integers: negative, positive and 0 integers. (e.g. …-2, -1, 0, 1,… 9999…)

Floating Point Numbers: real numbers. (e,g. 0.1, -12.2, 1.5x1012)

(43)

Number Representations

Unsigned Integers: positive and 0 integers. (e.g. 0, 1, 2, … 99999…

Signed Integers: negative, positive and 0 integers. (e.g. …-2, -1, 0, 1,… 9999…)

Floating Point Numbers: real numbers. (e,g. 0.1, -12.2, 1.5x1012)

Look up IEEE floating point if you’re interested!

(44)

Number Representations

C Declaration Size (Bytes)

int 4

double 8

float 4

char 1

char * 8

short 2

(45)

In The Days Of Yore…

C Declaration Size (Bytes)

int 4

double 8

float 4

char 1

char * 4

short 2

long 4

(46)

Transitioning To Larger Datatypes

Early 2000s: most computers were 32-bit. This means that pointers were 4 bytes (32 bits).

• 32-bit pointers store a memory address from 0 to 232-1, equaling 232 bytes of addressable memory. This equals 4 Gigabytes, meaning that 32-bit

computers could have at most 4GB of memory (RAM)!

Because of this, computers transitioned to 64-bit. This means that datatypes were enlarged; pointers in programs were now 64 bits.

(47)

Lecture Plan

• Bits and Bytes 5

• Hexadecimal 33

• Integer Representations 41

Unsigned Integers 47

• Signed Integers 51

• Overflow 77

• Casting and Combining Types 90

• Live Session 119

(48)

Unsigned Integers

An unsigned integer is 0 or a positive integer (no negatives).

• We have already discussed converting between decimal and binary, which is a nice 1:1 relationship. Examples:

0b0001 = 1 0b0101 = 5 0b1011 = 11 0b1111 = 15

• The range of an unsigned number is 0 → 2w- 1, where w is the number of bits.

E.g. a 32-bit integer can represent 0 to 232 – 1 (4,294,967,295).

(49)

Unsigned Integers

(50)

Let’s Take A Break

To ponder during the break:

A signed integer is a negative, 0, or positive

integer. How can we represent both negative

and positive numbers in binary?

(51)

Lecture Plan

• Bits and Bytes 5

• Hexadecimal 33

• Integer Representations 41

• Unsigned Integers 47

Signed Integers 51

• Overflow 77

• Casting and Combining Types 90

• Live Session 119

(52)

Signed Integers

A signed integer is a negative integer, 0, or a positive integer.

Problem: How can we represent negative and positive numbers in binary?

(53)

Signed Integers

A signed integer is a negative integer, 0, or a positive integer.

Problem: How can we represent negative and positive numbers in binary?

Idea: let’s reserve the most

significant bit to store the sign.

(54)

Sign Magnitude Representation

0110

positive 6

1011

(55)

Sign Magnitude Representation

0000

positive 0

1000

negative 0

🤯

(56)

Sign Magnitude Representation

• We’ve only represented 15 of our 16 available numbers!

1 000 = -0 1 001 = -1 1 010 = -2 1 011 = -3 1 100 = -4 1 101 = -5 1 110 = -6 1 111 = -7

0 000 = 0 0 001 = 1 0 010 = 2 0 011 = 3 0 100 = 4 0 101 = 5 0 110 = 6 0 111 = 7

(57)

Sign Magnitude Representation

Pro: easy to represent, and easy to convert to/from decimal.

Con: +-0 is not intuitive

Con: we lose a bit that could be used to store more numbers

Con: arithmetic is tricky: we need to find the sign, then maybe subtract

(borrow and carry, etc.), then maybe change the sign. This complicates the hardware support for something as fundamental as addition.

Can we do better?

(58)

A Better Idea

Ideally, binary addition would just work regardless of whether the number is positive or negative.

0101 ????

+ 0000

(59)

A Better Idea

Ideally, binary addition would just work regardless of whether the number is positive or negative.

0101 1011

+ 0000

(60)

A Better Idea

Ideally, binary addition would just work regardless of whether the number is positive or negative.

???? 0011

+ 0000

(61)

A Better Idea

Ideally, binary addition would just work regardless of whether the number is positive or negative.

0011 1101

+ 0000

(62)

A Better Idea

Ideally, binary addition would just work regardless of whether the number is positive or negative.

0000 ????

+ 0000

(63)

A Better Idea

Ideally, binary addition would just work regardless of whether the number is positive or negative.

0000 0000

+ 0000

(64)

A Better Idea

Decimal Positive Negative

0 0000 0000

1 0001 1111

2 0010 1110

3 0011 1101

4 0100 1100

5 0101 1011

Decimal Positive Negative

8 1000 1000

9 1001 (same as -7!) NA

10 1010 (same as -6!) NA

11 1011 (same as -5!) NA

12 1100 (same as -4!) NA

13 1101 (same as -3!) NA

(65)

There Seems Like a Pattern Here…

0101 1011

+ 0000

0011 1101

+ 0000

0000 0000

+ 0000

The negative number is the positive number inverted, plus one!

(66)

There Seems Like a Pattern Here…

A binary number plus its inverse is all 1s. Add 1 to this to carry over all 1s and get 0!

0101 1010

+ 1111

0001 1111

+ 0000

(67)

Another Trick

• To find the negative equivalent of a number, work right-to-left and write down all digits through when you reach a 1. Then, invert the rest of the digits.

100100

??????

000000

+

(68)

Another Trick

• To find the negative equivalent of a number, work right-to-left and write down all digits through when you reach a 1. Then, invert the rest of the digits.

100100

???100 000000

+

(69)

Another Trick

• To find the negative equivalent of a number, work right-to-left and write down all digits through when you reach a 1. Then, invert the rest of the digits.

100100 011100 000000

+

(70)

Two’s Complement

(71)

Two’s Complement

In two’s complement, we represent a positive number as itself, and its

negative equivalent as the two’s complement of itself.

The two’s complement of a number is the binary digits inverted, plus 1.

• This works to convert from positive to negative, and back from negative to positive!

(72)

Two’s Complement

Con: more difficult to represent, and

difficult to convert to/from decimal and between positive and negative.

Pro: only 1 representation for 0!

Pro: all bits are used to represent as many numbers as possible

Pro: the most significant bit still indicates the sign of a number.

Pro: addition works for any combination

(73)

Two’s Complement

• Adding two numbers is just…adding! There is no special case needed for negatives. E.g. what is 2 + -5?

0010 1011

+ 1101

2

-5

-3

(74)

Two’s Complement

• Subtracting two numbers is just performing the two’s complement on one of them and then adding. E.g. 4 – 5 = -1.

0100 0101

-

4 5

0100 1011

+ 1111

4

-5

-1

(75)

Practice: Two’s Complement

What are the negative or positive equivalents of the numbers below?

a) -4 (1100) b) 7 (0111) c) 3 (0011) d) -8 (1000)

(76)

Practice: Two’s Complement

What are the negative or positive equivalents of the numbers below?

a) -4 (1100) b) 7 (0111) c) 3 (0011)

(77)

Lecture Plan

• Bits and Bytes 5

• Hexadecimal 33

• Integer Representations 41

• Unsigned Integers 47

• Signed Integers 51

Overflow 77

• Casting and Combining Types 90

• Live Session 119

(78)

Overflow

If you exceed the maximum value of your bit representation, you wrap around or overflow back to the smallest bit representation.

0b1111 + 0b1 = 0b0000

If you go below the minimum value of your bit representation, you wrap around or overflow back to the largest bit representation.

0b0000 - 0b1 = 0b1111

(79)

Min and Max Integer Values

Type Size

(Bytes) Minimum Maximum

char 1 -128 127

unsigned char 1 0 255

short 2 -32768 32767

unsigned short 2 0 65535

int 4 -2147483648 2147483647

unsigned int 4 0 4294967295

long 8 -9223372036854775808 9223372036854775807

(80)

Min and Max Integer Values

INT_MIN, INT_MAX, UINT_MAX, LONG_MIN, LONG_MAX, ULONG_MAX, …

(81)

Overflow

000…000 111…111

011…111 100…000

000…001

000…010 000…011 111…110

111…101 111…100

100…001 100…010

011…110

011…101 +1 +1

(82)

Overflow

At which points can overflow occur for signed and unsigned int? (assume binary values shown are all 32 bits)

A. Signed and unsigned can both overflow at points X and Y

B. Signed can overflow only at X, unsigned only at Y

C. Signed can overflow only at Y, unsigned only at X

D. Signed can overflow at X and Y, unsigned only at X

X

Y

000…000 111…111

000…001

000…010 000…011 111…110

111…101 111…100

(83)

Unsigned Integers

000…000 111…111

011…111 100…000

000…001

000…010 000…011 111…110

111…101 111…100

100…001 100…010

011…110

011…101

≈+4billion 0

Discontinuity means overflow possible here

Increasing positivenumbers

More increasing positivenumbers

(84)

Signed Numbers

000…000 111…111

000…001

000…010 000…011 111…110

111…101 111…100

100…010 011…101

-1 0

Discontinuity means overflow possible here

Increasing positive numbers

ative numbersbecoming less negative increasing)

+1

(85)

Overflow In Practice: PSY

YouTube: “We never thought a video would be watched in numbers

greater than a 32-bit integer (=2,147,483,647 views), but that was before we met PSY. "Gangnam Style" has been viewed so many times we had to upgrade to a 64-bit integer (9,223,372,036,854,775,808)!”

(86)

Overflow In Practice: Timestamps

Many systems store timestamps as the number of seconds since Jan. 1, 1970 in a signed 32-bit integer.

Problem: the latest timestamp that can be represented this way is 3:14:07 UTC on Jan. 13 2038!

(87)

Overflow In Practice: Gandhi

• In the game “Civilization”, each civilization leader had an

“aggression” rating. Gandhi was meant to be peaceful, and had a score of 1.

• If you adopted “democracy”, all players’ aggression reduced by 2.

Gandhi’s went from 1 to 255!

• Gandhi then became a big fan of nuclear weapons.

https://kotaku.com/why-gandhi-is-such-an-asshole-in-civilization-1653818245

(88)

Overflow in Practice:

• Pacman Level 256

• Make sure to reboot Boeing Dreamliners every 248 days

• Comair/Delta airline had to cancel thousands of flights days before Christmas

• Reported vulnerability CVE-2019-3857 in libssh2 may allow a hacker to remotely execute code

• Donkey Kong Kill Screen

(89)

Demo Revisited:

Unexpected Behavior

(90)

Lecture Plan

• Bits and Bytes 5

• Hexadecimal 33

• Integer Representations 41

• Unsigned Integers 47

• Signed Integers 51

• Overflow 77

Casting and Combining Types 90

• Live Session 119

(91)

printf and Integers

• There are 3 placeholders for 32-bit integers that we can use:

%d: signed 32-bit int

%u: unsigned 32-bit int

%x: hex 32-bit int

The placeholder—not the expression filling in the placeholder—dictates what gets printed!

(92)

Casting

What happens at the byte level when we cast between variable types? The bytes remain the same! This means they may be interpreted differently depending on the type.

int v = -12345;

unsigned int uv = v;

printf("v = %d, uv = %u\n", v, uv);

This prints out: "v = -12345, uv = 4294954951". Why?

(93)

Casting

What happens at the byte level when we cast between variable types? The bytes remain the same! This means they may be interpreted differently depending on the type.

int v = -12345;

unsigned int uv = v;

printf("v = %d, uv = %u\n", v, uv);

The bit representation for -12345 is

0b11111111111111111100111111000111.

(94)

Casting

(95)

Comparisons Between Different Types

Be careful when comparing signed and unsigned integers. C will implicitly cast the signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.

Expression Type Evaluation Correct?

0 == 0U -1 < 0 -1 < 0U

2147483647 > - 2147483647 - 1 2147483647U > - 2147483647 - 1 2147483647 >

(int)2147483648U

(96)

Comparisons Between Different Types

Be careful when comparing signed and unsigned integers. C will implicitly cast the signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.

Expression Type Evaluation Correct?

0 == 0U Unsigned 1 yes

-1 < 0 -1 < 0U

2147483647 > - 2147483647 - 1 2147483647U > - 2147483647 - 1

(97)

Comparisons Between Different Types

Be careful when comparing signed and unsigned integers. C will implicitly cast the signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.

Expression Type Evaluation Correct?

0 == 0U Unsigned 1 yes

-1 < 0 Signed 1 yes

-1 < 0U

2147483647 > - 2147483647 - 1 2147483647U > - 2147483647 - 1 2147483647 >

(int)2147483648U

(98)

Comparisons Between Different Types

Be careful when comparing signed and unsigned integers. C will implicitly cast the signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.

Expression Type Evaluation Correct?

0 == 0U Unsigned 1 yes

-1 < 0 Signed 1 yes

-1 < 0U Unsigned 0 No!

2147483647 > - 2147483647 - 1 2147483647U > - 2147483647 - 1

(99)

Comparisons Between Different Types

Be careful when comparing signed and unsigned integers. C will implicitly cast the signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.

Expression Type Evaluation Correct?

0 == 0U Unsigned 1 yes

-1 < 0 Signed 1 yes

-1 < 0U Unsigned 0 No!

2147483647 > -

2147483647 - 1 Signed 1 yes

2147483647U > - 2147483647 - 1 2147483647 >

(int)2147483648U

(100)

Comparisons Between Different Types

Be careful when comparing signed and unsigned integers. C will implicitly cast the signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.

Expression Type Evaluation Correct?

0 == 0U Unsigned 1 yes

-1 < 0 Signed 1 yes

-1 < 0U Unsigned 0 No!

2147483647 > -

2147483647 - 1 Signed 1 yes

2147483647U > -

2147483647 - 1 Unsigned 0 No!

(101)

Comparisons Between Different Types

Be careful when comparing signed and unsigned integers. C will implicitly cast the signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.

Expression Type Evaluation Correct?

0 == 0U Unsigned 1 yes

-1 < 0 Signed 1 yes

-1 < 0U Unsigned 0 No!

2147483647 > -

2147483647 - 1 Signed 1 yes

2147483647U > -

2147483647 - 1 Unsigned 0 No!

2147483647 >

(int)2147483648U Signed 1 No!

(102)

Comparisons Between Different Types

Be careful when comparing signed and unsigned integers. C will implicitly cast the signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.

Expression Type Evaluation Correct?

0 == 0U Unsigned 1 yes

-1 < 0 Signed 1 yes

-1 < 0U Unsigned 0 No!

2147483647 > -

2147483647 - 1 Signed 1 yes

2147483647U > -

2147483647 - 1 Unsigned 0 No!

(103)

Comparisons Between Different Types

Be careful when comparing signed and unsigned integers. C will implicitly cast the signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.

Expression Type Evaluation Correct?

0 == 0U Unsigned 1 yes

-1 < 0 Signed 1 yes

-1 < 0U Unsigned 0 No!

2147483647 > -

2147483647 - 1 Signed 1 yes

2147483647U > -

2147483647 - 1 Unsigned 0 No!

2147483647 >

(int)2147483648U Signed 1 No!

Signed 1 yes

(104)

Comparisons Between Different Types

Which many of the following statements are true? (assume that variables are set to values that place them in the spots shown)

s3 > u3 u2 > u4 s2 > s4 s1 > s2 u1 > u2 s1 > u3

(105)

Comparisons Between Different Types

Which many of the following statements are true? (assume that variables are set to values that place them in the spots shown)

s3 > u3 - true u2 > u4

s2 > s4 s1 > s2 u1 > u2 s1 > u3

(106)

Comparisons Between Different Types

Which many of the following statements are true? (assume that variables are set to values that place them in the spots shown)

s3 > u3 - true u2 > u4 - true s2 > s4

s1 > s2 u1 > u2 s1 > u3

(107)

Comparisons Between Different Types

Which many of the following statements are true? (assume that variables are set to values that place them in the spots shown)

s3 > u3 - true u2 > u4 - true s2 > s4 - false s1 > s2

u1 > u2 s1 > u3

(108)

Comparisons Between Different Types

Which many of the following statements are true? (assume that variables are set to values that place them in the spots shown)

s3 > u3 - true u2 > u4 - true s2 > s4 - false s1 > s2 - true u1 > u2

s1 > u3

(109)

Comparisons Between Different Types

Which many of the following statements are true? (assume that variables are set to values that place them in the spots shown)

s3 > u3 - true u2 > u4 - true s2 > s4 - false s1 > s2 - true u1 > u2 - true s1 > u3

(110)

Comparisons Between Different Types

Which many of the following statements are true? (assume that variables are set to values that place them in the spots shown)

s3 > u3 - true u2 > u4 - true s2 > s4 - false s1 > s2 - true u1 > u2 - true s1 > u3 - true

(111)

Expanding Bit Representations

• Sometimes, we want to convert between two integers of different sizes (e.g.

short to int, or int to long).

• We might not be able to convert from a bigger data type to a smaller data

type, but we do want to always be able to convert from a smaller data type to a bigger data type.

For unsigned values, we can add leading zeros to the representation (“zero extension”)

For signed values, we can repeat the sign of the value for new digits (“sign extension”

• Note: when doing <, >, <=, >= comparison between different size types, it will promote to the larger type.

(112)

Expanding Bit Representation

unsigned short s = 4;

// short is a 16-bit format, so s = 0000 0000 0000 0100b

unsigned int i = s;

// conversion to 32-bit int, so i = 0000 0000 0000 0000 0000 0000 0000 0100b

(113)

Expanding Bit Representation

short s = 4;

// short is a 16-bit format, so s = 0000 0000 0000 0100b

int i = s;

// conversion to 32-bit int, so i = 0000 0000 0000 0000 0000 0000 0000 0100b

— or —

short s = -4;

// short is a 16-bit format, so s = 1111 1111 1111 1100b

int i = s;

(114)

Truncating Bit Representation

If we want to reduce the bit size of a number, C truncates the representation and discards the more significant bits.

What happens here? Let's look at the bits in x (a 32-bit int), 53191:

0000 0000 0000 0000 1100 1111 1100 0111

When we cast x to a short, it only has 16-bits, and C truncates the number:

int x = 53191;

short sx = x;

int y = sx;

(115)

Truncating Bit Representation

If we want to reduce the bit size of a number, C truncates the representation and discards the more significant bits.

What happens here? Let's look at the bits in x (a 32-bit int), -3:

1111 1111 1111 1111 1111 1111 1111 1101

When we cast x to a short, it only has 16-bits, and C truncates the number:

1111 1111 1111 1101

This is -3! If the number does fit, it will convert fine. y looks like this:

int x = -3;

short sx = x;

int y = sx;

(116)

Truncating Bit Representation

If we want to reduce the bit size of a number, C truncates the representation and discards the more significant bits.

What happens here? Let's look at the bits in x (a 32-bit unsigned int), 128000:

0000 0000 0000 0001 1111 0100 0000 0000

When we cast x to a short, it only has 16-bits, and C truncates the number:

unsigned int x = 128000;

unsigned short sx = x;

unsigned int y = sx;

(117)

The sizeof Operator

long sizeof(type);

// Example

long int_size_bytes = sizeof(int); // 4

long short_size_bytes = sizeof(short); // 2 long char_size_bytes = sizeof(char); // 1

sizeof takes a variable type as a parameter and returns the size of that type, in bytes.

(118)

Recap

• Bits and Bytes

• Hexadecimal

• Integer Representations

• Unsigned Integers

• Signed Integers

• Overflow

• Casting and Combining Types

(119)

Additional Live Session

Slides

(120)

Live Session

• Optional, led by what is most helpful for us to review!

• Video and slides posted

• Post any lecture questions while watching videos in our Ed thread for lecture

(121)

Plan For Today

First 5 minutes: post questions or comments on Ed for what we should discuss!

Lecture 2 takeaway: computers represent everything in binary. We must determine how to represent our data

(e.g., base-10 numbers) in a binary format so a

computer can manipulate it. There may be limitations to

these representations! (overflow)

(122)

Practice: Two’s Complement

Fill in the below table:

char x = ____; char y = -x;

decimal binary decimal binary

1. 0b1111 1100

2. 0b0001 1000

3. 0b0010 0100

It’s easier to compute base-10 for positive numbers, so use two’s complement first if negative.

(123)

Practice: Two’s Complement

Fill in the below table:

char x = ____; char y = -x;

decimal binary decimal binary

1. 0b1111 1100

2. 0b0001 1000

3. 0b0010 0100

4. 0b1101 1111

4 0b0000 0100 -4

It’s easier to compute base-10 for positive numbers, so use two’s complement first if negative.

🤔

(124)

Practice: Two’s Complement

Fill in the below table:

char x = ____; char y = -x;

decimal binary decimal binary

1. 0b1111 1100

2. 0b0001 1000

3. 0b0010 0100

4 -24 -36

0b0000 0100 0b1110 1000 -4

24

It’s easier to compute base-10 for positive numbers, so use two’s complement first if negative.

(125)

History: Two’s complement

• The binary representation was first proposed by John von Neumann in First Draft of a Report on the EDVAC (1945)

That same year, he also invented the merge sort algorithm

• Many early computers used sign-magnitude or

one’s complement

• The System/360, developed by IBM in 1964, was widely popular (had 1024KB memory) and

established two’s complement as the dominant binary representation of integers

EDSAC (1949)

8-bit one’s complement

+7 0b0000 0111 -7 0b1111 1000

(126)

Hexadecimal: It’s funky but concise

• Let’s take a byte (8 bits):

0b10100101 165

0xa5

Base-10: Human-readable,

but cannot easily interpret on/off bits Base-2: Yes, computers use this,

but not human-readable

Base-16: Easy to convert to Base-2,

More “portable” as a human-readable format

(127)

Hexadecimal and Truncation

For each initialization of x, what will be printed?

i. x = 130; // 0x82

ii. x = -132; // 0xff7c

iii. x = 25; // 0x19

short x = ______;

char cx = x;

printf("%d", cx);

🤔

(128)

Hexadecimal and Truncation

For each initialization of x, what will be printed?

i. x = 130; // 0x82

ii. x = -132; // 0xff7c

iii.

-126

124

short x = ______;

char cx = x;

printf("%d", cx);

(129)

Signed vs. Unsigned Integers

0 1 2

8 7 10 9

15

13 14

11

12 4

5 6

3

(130)

Underspecified question

What is the following base-2 number in base-10?

0 1 2

8 7 10 9

15

13 14

11

12 4

5 6

3

0b1101

(131)

Underspecified question

What is the following base-2 number in base-10?

0 1 2

8 7 10 9

15

13 14

11

12 4

5 6

3

0b1101

You need to know the type to determine the number! (Note by default, numeric constants in C are signed ints)

If 4-bit signed: -3

If 4-bit unsigned: 13 If >4-bit signed or unsigned: 13

(132)

Overflow

• What is happening here? Assume 4-bit numbers.

0 1 2

8 7 10 9

15

13 14

11

12 4

5 6

3

0b1101

0b0100

+

(133)

Overflow

• What is happening here? Assume 4-bit numbers.

0b1101 0b0100

+

0

1 2

8 7 10 9

15

13 14

11

12 4

5 6

3

Signed

-3 + 4 = 1 No overflow

Unsigned 13 + 4 = 1

Overflow

(134)

Limits and Comparisons

1. What is the…

2. Will the following char comparisons evaluate to true or false?

i. -7 < 4 ii.

iii. (char) 130 > 4 iv.

Largest unsigned? Largest signed? Smallest signed?

char int

(135)

Limits and Comparisons

1. What is the…

These are available as UCHAR_MAX, INT_MIN, INT_MAX, etc. in the

<limits.h> header.

Largest unsigned? Largest signed? Smallest signed?

char int

28 - 1 = 255 27 – 1 = 127 -27 = -128 232 - 1 =

4294967296

231 - 1 =

2147483647

-231 =

-2147483648

(136)

Limits and Comparisons

2. Will the following char comparisons evaluate to true or false?

i. -7 < 4

ii. -7 < 4U

iii. (char) 130 > 4

iv. (char) -132 > 2 true

false

false

true

(137)

Tools: A binary/hex calculator

Yes. Next week, we will learn more about gdb, our debugger.

• gdb can print out variables/constants in any format: hex, decimal, unsigned…

• To look ahead, check out our GDB guide and read about print format codes.

Or watch Lecture 3!

🤔

Is there a program to quickly convert between hex, binary,

and decimal?

References

Related documents

This study is an examination of the individual language attitudes and motivations for learning Afrikaans as expressed by two groups of secondary school learners at the same school

b Indian Head Massage decreases the release of endorphins from the brain c Indian Head Massage increases the blood flow to the head, neck and shoulders d Indian Head Massage

Through the death of the man Jesus, they would have the very quality of life, divine life, revealed in his death.. Jesus was calling them to radical discipleship that is similar

In the above, the processing functions are written using \v1

The first combined training and the strategy design workshop will be carried out with the Strategy Task Groups in all project regions, planed from May 30 th to June 3 rd 2005

After creating the metadata for an entity type, you can use the Generate Jobs option from the entity type editor toolbar to create and publish jobs to the DataFlux Data

estimates in regional and global observed temperature changes: A new data set from

Organizations Business Statistics Macroeconomics for Global Economy Module 2 Operations Management EXECUTIVE SEMINARS CAPSTONE PROJECT Financial Accounting MODULE COURSES