Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hexadecimal, Signed Numbers, and Arbitrariness

Similar presentations


Presentation on theme: "Hexadecimal, Signed Numbers, and Arbitrariness"— Presentation transcript:

1 Hexadecimal, Signed Numbers, and Arbitrariness
CS/COE 0447 (term 2181) Jarrett Billingsley

2 Class Announcements No recitations tomorrow or Monday!
Recitations will start next week with an intro to using MARS. Add/drop ends next Friday, September 8th. My office has changed to 6510 SENSQ. Let's do a quick review of converting to/from binary! 8/31/2017 CS/COE term 2181

3 Hexadecimal 8/31/2017 CS/COE term 2181

4 Shortcomings of binary and decimal
Binary numbers can get really long, really quickly. 3,927,66410 = But nice "round" numbers in binary look arbitrary in decimal. = 32,76810 This is because 10 is not a power of 2! We could use base-4, base-8, base-16, base-32, etc. Base-4 is not much terser than binary e.g. 3,927,66410 = Base-32 would require 32 digit symbols. Yeesh. Base-8 and base-16 look promising! 8/31/2017 CS/COE term 2181

5 Let's make a base-2 16 number system
Given base B, There are B digit symbols Each place is worth Bi, starting with i = 0 on the right Given n digits, You can represent Bn numbers The largest representable number is Bn – 1 So how about base-16? 8/31/2017 CS/COE term 2181

6 Hexadecimal, or "hex" (base-16)
Digit symbols after 9 are A-F, meaning respectively. Usually we call one hexadecimal digit a hex digit. No fancy name :( 0 × 167 + 0 × 166 + 3 × 165 + 11 × 164 + 14 × 163 + 14 × 162 + 7 × 161 + 0 × 160 = 3,927,66410 003B EE70 = To convert hex to decimal: use a dang calculator lol 8/31/2017 CS/COE term 2181

7 The relationship between binary and hex
Four bits are equivalent to one hex digit. Converting between them is easy! Say we had this binary number: Starting from the LSB, divide into groups of 4 bits (put 0s before the first digits if there are leftovers). Then use the table. Bin Hex 0000 1000 8 0001 1 1001 9 0010 2 1010 A 0011 3 1011 B 0100 4 1100 C 0101 5 1101 D 0110 6 1110 E 0111 7 1111 F 0x 3 B E E 7 (this is common notation for hex, derived from the C language.) 8/31/2017 CS/COE term 2181

8 Powers of Two Memorize at least the powers up to 28.
These are the place values for binary, and they are also nice "round" numbers in binary and hex. What is the largest number that an 8-bit value can hold? What is that in hexadecimal? 255: 0xFF How about a 16-bit value? 65535: 0xFFFF "0xFFFF" is kinda like "9999" in decimal. What happens if we go beyond that? Dec Hex 21 2 0x2 22 4 0x4 23 8 0x8 24 16 0x10 25 32 0x20 26 64 0x40 27 128 0x80 28 256 0x100 8/31/2017 CS/COE term 2181

9 Overflow In computers, numbers are finite.
Let's say our 4-digit display was counting up: 9997, 9998, 9999… What comes "next"? What does this "0000" really mean? It wrapped around. This is overflow: the number you are trying to represent is too big to be represented. Essentially, all arithmetic on the computer is modular arithmetic! This causes a lot of software bugs. 8/31/2017 CS/COE term 2181

10 Signed Numbers 8/31/2017 CS/COE term 2181

11 Adding in binary It works the same way as you learned in school, except instead of carrying at 1010, you carry at… 102! 1 + 1 = 102 (210) = 112 (310) Now let's try it. (What are these in decimal?) 8/31/2017 CS/COE term 2181

12 If you wanted to implement signed integers…
What would be the most intuitive way of doing it? You could add a "sign bit" where 0 means +, and 1 means -. We call this sign-magnitude representation. On a number line: Cool! But what about the pattern 100? Negative sign, 0 magnitude. TWO ZEROES?? Arithmetic is also a bit awkward. For these reasons, sign-magnitude is not used for integers on most computers today. (but it is used for floats!) 110 -2 000 010 +2 111 -3 101 -1 001 +1 011 +3 100 8/31/2017 CS/COE term 2181

13 Another dead end -2 +2 -3 -1 +1 +3 101 000 010 100 110 001 011 111
In a technique borrowed from old accounting practices and mechanical calculators, we can flip all the bits to negate a number. We call this one's complement. On a number line: We have the same problem with 111 as sign-magnitude had with 100. This does make arithmetic easier, but there are still complications, and this has not been used in computers since the early days. (Don't worry about remembering how one's complement works. Just know that it exists and it has shortcomings!) 101 -2 000 010 +2 100 -3 110 -1 001 +1 011 +3 111 8/31/2017 CS/COE term 2181

14 Our friend, two's complement
In this system, to negate, we flip all the bits, then add 1. The number line looks a little bit stranger… There is only one 0, and it is 0! But it's a bit lopsided… There are more negatives than positives! The most-negative number has no matching positive number. What happens if you try to negate -4 here? Despite this, 2's complement is universal in modern computers. 100 -4 110 -2 000 010 +2 101 -3 111 -1 001 +1 011 +3 8/31/2017 CS/COE term 2181

15 Chopping up number lines
Here are number lines for unsigned and signed 3-bit integers. What happened to the bit patterns for 4 through 7? Notice that with 3 bits, we can still only represent 23 values. But instead of 0 to 23-1… It's -22 to 22-1 Intuitively, think "half the values are negative." An unsigned 8-bit number has the range 0 to 28-1. What's the range of a signed 8-bit number? 110 6 101 5 000 001 1 010 2 011 3 111 7 100 4 110 -2 101 -3 000 001 +1 010 +2 011 +3 111 -1 100 -4 8/31/2017 CS/COE term 2181

16 Zero- and sign-extension
Sometimes you need to widen a number with fewer bits to more. For unsigned numbers, this is easy: put 0s at the beginning. 1001  to 8 bits  We call that zero extension. If you did that with signed numbers, it wouldn't work right. What would the signed interpretations of the above be? Instead, we sign-extend signed numbers by putting copies of the sign bit at the beginning. 1001  to 8 bits  0010  to 8 bits  8/31/2017 CS/COE term 2181

17 Two's complement addition
The great thing is: you can add numbers of either sign without having to do anything special! 1 1 to binary? positive 6? 3 +10 13 0011 3 +-6 -3 0110 0011 - 3 +1010 flip! +1010 +1 to decimal? 1001 1101 1101 0011 flip! Clearly the actual patterns of bits are the same. So how does the computer "know" whether it's doing signed or unsigned addition? +1 0010 8/31/2017 CS/COE term 2181

18 IT DOESN'T 8/31/2017 CS/COE term 2181

19 Arbitrariness 8/31/2017 CS/COE term 2181

20 What it means to be "arbitrary"
Something is "arbitrary" if there is no reason for it to be that way. We just agree that it's how things are. One of the biggest things I want you to know is: What a pattern of bits means is arbitrary. As a corollary: The same pattern of bits can be interpreted many different ways. 8/31/2017 CS/COE term 2181

21 One bit pattern, many possible meanings
All information on computers is stored as patterns of bits. How these bits are interpreted, transformed, and displayed is up to the programmer and the user. -59 Signed integer R3G3B2 color 196 Unsigned integer 0xC4 Hexadecimal call nz z80 instruction Ä Unicode 8/31/2017 CS/COE term 2181

22 The computer doesn't know or care.
When writing assembly (and C!) programs, the computer has no idea what you mean, cause nothing means anything to it. To the computer, there is no difference between nonsense code and useful code. It's all up to you to make sure your programs are well-structured, well-commented, and behave correctly in all cases. Welcome to low-level programming :D 8/31/2017 CS/COE term 2181


Download ppt "Hexadecimal, Signed Numbers, and Arbitrariness"

Similar presentations


Ads by Google