Hexadecimal, Signed Numbers, and Arbitrariness

Slides:



Advertisements
Similar presentations
Week 3. Assembly Language Programming  Difficult when starting assembly programming  Have to work at low level  Use processor instructions >Requires.
Advertisements

Representing Numbers: Integers
COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
Assembly Language and Computer Architecture Using C++ and Java
1  2004 Morgan Kaufmann Publishers Chapter Three.
Agenda Shortcuts converting among numbering systems –Binary to Hex / Hex to Binary –Binary to Octal / Octal to Binary Signed and unsigned binary numbers.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Assembly Language and Computer Architecture Using C++ and Java
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
+ CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
Binary Arithmetic Math For Computers.
Binary Representation - Shortcuts n Negation x + x = 1111…1111 two = -1 (in 2’s complement) Therefore, -x = x + 1 n Sign Extension o Positive numbers :
CENG 311 Machine Representation/Numbers
Number Representations and Computer Arithmetic. CS 21a 9/23/02 Odds and Ends Slide 2 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University.
Lecture 5.
IT253: Computer Organization
Computing Systems Basic arithmetic for computers.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
1 Data Representation Characters, Integers and Real Numbers Binary Number System Octal Number System Hexadecimal Number System Powered by DeSiaMore.
Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)
Chapter 2 Number Systems: Decimal, Binary, and Hex.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise.
Data Representation. Goal: Store numbers, characters, sets, database records in the computer. What we got: Circuit that stores 2 voltages, one for logic.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
순천향대학교 정보기술공학부 이 상 정 1 3. Arithmetic for Computers.
Computer Organization 1 Data Representation Negative Integers.
Representing Positive and Negative Numbers
Based on slides from D. Patterson and www-inst.eecs.berkeley.edu/~cs152/ COM 249 – Computer Organization and Assembly Language Chapter 3 Arithmetic For.
Data Representation COE 308 Computer Architecture
Cosc 2150: Computer Organization
Binary & Hex Review.
Design of Digital Circuits Reading: Binary Numbers
Chapter 2 Binary Number Systems.
David Kauchak CS 52 – Spring 2017
Bits, Data Types, and Operations
Binary, Denary, Hexadecimal Conversion Binary Addition
Data Representation ICS 233
Lec 3: Data Representation
Data Representation.
A brief comparison of integer and double representation
3.1 Denary, Binary and Hexadecimal Number Systems
Data Representation Binary Numbers Binary Addition
Lesson Objectives Aims
Data Representation in Computer Systems
Binary Addition and Subtraction
Lesson objectives Understand how computers represent and manipulate numbers [unsigned integers, signed integers (sign and magnitude, Two’s complement)
University of Gujrat Department of Computer Science
Binary, Hex, and Arbitrariness
Wakerly Section 2.4 and further
Binary, Hex, and Arbitrariness
Addition, Signed Numbers, and Overflow
Data Representation Data Types Complements Fixed Point Representation
Numbers and Arithmetic
Subtraction The arithmetic we did so far was limited to unsigned (positive) integers. Today we’ll consider negative numbers and subtraction. The main problem.
EEL 3705 / 3705L Digital Logic Design
CS 101 – Sept. 4 Number representation Integer Unsigned √ Signed √
COMS 161 Introduction to Computing
Instructor: Alexander Stoytchev
Decimal and binary representation systems
Data Representation ICS 233
COMS 161 Introduction to Computing
Binary & Hex Review.
Data Representation COE 308 Computer Architecture
Presentation transcript:

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

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 0447 term 2181

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

Shortcomings of binary and decimal Binary numbers can get really long, really quickly. 3,927,66410 = 11 1011 1110 1110 0111 00002 But nice "round" numbers in binary look arbitrary in decimal. 10000000000000002 = 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 = 120 3331 2323 00004 Base-32 would require 32 digit symbols. Yeesh. Base-8 and base-16 look promising! 8/31/2017 CS/COE 0447 term 2181

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 0447 term 2181

Hexadecimal, or "hex" (base-16) Digit symbols after 9 are A-F, meaning 10-15 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 = 167 166 165 164 163 162 161 160 To convert hex to decimal: use a dang calculator lol 8/31/2017 CS/COE 0447 term 2181

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: 11101111101110011100002 Starting from the LSB, divide into groups of 4 bits (put 0s before the first digits if there are leftovers). Then use the table. 0011 1011 1110 1110 0111 0000 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 0447 term 2181

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 0447 term 2181

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 0447 term 2181

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

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) 1 + 1 + 1 = 112 (310) Now let's try it. (What are these in decimal?) 1011 0010 +0010 1111 8/31/2017 CS/COE 0447 term 2181

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 0447 term 2181

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 0447 term 2181

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 0447 term 2181

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 0447 term 2181

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  0000 1001 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  1111 1001 0010  to 8 bits  0000 0010 8/31/2017 CS/COE 0447 term 2181

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 0447 term 2181

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

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

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 0447 term 2181

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 11000100 196 Unsigned integer 0xC4 Hexadecimal call nz z80 instruction Ä Unicode 8/31/2017 CS/COE 0447 term 2181

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 0447 term 2181