CS 261 Fall Mike Lam, Professor Integer Encodings

Similar documents
COMP2611: Computer Organization. Data Representation

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

World Inside a Computer is Binary

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Representation of Non Negative Integers

CS 64 Week 1 Lecture 1. Kyle Dewey

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

Number System. Introduction. Decimal Numbers

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Introduction to Computers and Programming. Numeric Values

Integers and Floating Point

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

Number Systems (2.1.1)

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

Part 2,Number Systems Questions

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Chapter 3: Arithmetic for Computers

Arithmetic and Bitwise Operations on Binary Data

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

CS101 Lecture 04: Binary Arithmetic

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

SIGNED AND UNSIGNED SYSTEMS

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Binary Representations and Arithmetic

Signed Binary Numbers

Inf2C - Computer Systems Lecture 2 Data Representation

Arithmetic and Bitwise Operations on Binary Data

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

COMP 122/L Lecture 2. Kyle Dewey

CS 270: Computer Organization. Integer Arithmetic Operations

Introduction to Computer Science. Homework 1

Chapter 4: Data Representations

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

M1 Computers and Data

ECE/Comp Sci 352 Digital Systems Fundamentals. Charles R. Kime Section 2 Fall Logic and Computer Design Fundamentals

Integer Representation

Operations, Operands, and Instructions

Chapter 3: part 3 Binary Subtraction

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Numeric Encodings Prof. James L. Frankel Harvard University

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

Number Systems. Both numbers are positive

1.1. Unit 1. Integer Representation

Question 4: a. We want to store a binary encoding of the 150 original Pokemon. How many bits do we need to use?

CS 101: Computer Programming and Utilization

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

More about Binary 9/6/2016

Integer Representation

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Lecture 6: Signed Numbers & Arithmetic Circuits. BCD (Binary Coded Decimal) Points Addressed in this Lecture

Representation of Numbers

Computer Organization

Lecture 8: Addition, Multiplication & Division

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Binary Adders: Half Adders and Full Adders

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

COMP Overview of Tutorial #2

Binary Addition. Add the binary numbers and and show the equivalent decimal addition.

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

NUMBER OPERATIONS. Mahdi Nazm Bojnordi. CS/ECE 3810: Computer Organization. Assistant Professor School of Computing University of Utah

Chapter 4. Combinational Logic

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

EE 109 Unit 6 Binary Arithmetic

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

The type of all data used in a C (or C++) program must be specified

Floating Point Numbers

Floating Point Numbers

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Chapter 2. Data Representation in Computer Systems

Foundations of Computer Systems

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Admin. ! Assignment 3. ! due Monday at 11:59pm! one small error in 5b (fast division) that s been fixed. ! Midterm next Thursday in-class (10/1)

Chapter 1. Digital Systems and Binary Numbers

Arithmetic Processing

Module 2: Computer Arithmetic

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Data Representation. DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Advanced Computer Architecture-CS501

MACHINE LEVEL REPRESENTATION OF DATA

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

THE LOGIC OF COMPOUND STATEMENTS

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Binary Arithmetic. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T.

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

Data III & Integers I

Integers Sep 3, 2002

Topics of this Slideset. CS429: Computer Organization and Architecture. Encoding Integers: Unsigned. C Puzzles. Integers

CS429: Computer Organization and Architecture

Transcription:

CS 261 Fall 2018 Mike Lam, Professor https://xkcd.com/571/ Integer Encodings

Integers Topics C integer data types Unsigned encoding Signed encodings Conversions

Integer data types in C99 1 byte 2 bytes 2 bytes 4 bytes 4 bytes 8 bytes

Integer data types on stu All sizes in bytes; sizes in red are larger than mandated by C99 char 1 unsigned char 1 short 2 unsigned short 2 int 4 unsigned int 4 long 8 unsigned long 8 long long 8 unsigned long long 8 int8_t 1 uint8_t 1 bool 1 int16_t 2 uint16_t 2 int32_t 4 uint32_t 4 int64_t 8 uint64_t 8 size_t 8

Unsigned integer encoding Bit i represents the value 2i Bits typically written from most to least significant (i.e., 2 3 2 2 2 1 2 0 ) This is the same encoding we saw on Tuesday! No representation of negative numbers 1 = 1 = 0 2 3 + 0 2 2 + 0 2 1 + 1 2 0 = [0001] 5 = 4 + 1 = 0 2 3 + 1 2 2 + 0 2 1 + 1 2 0 = [0101] 11 = 8 + 2 + 1 = 1 2 3 + 0 2 2 + 1 2 1 + 1 2 0 = [1011] 15 = 8 + 4 + 2 + 1 = 1 2 3 + 1 2 2 + 1 2 1 + 1 2 0 = [1111]

Unsigned integer encoding Textbook s notation Each bar represents a bit Add together bars to represent the contributions of each bit value to the overall value

Signed integer encodings Sign magnitude Most natural and intuitive Ones complement Helps with two s complement conversions Two s complement Cleanest arithmetic but not intuitive Most modern signed integer types use this!

Sign magnitude Sign magnitude Interpret most-significant bit as a sign bit Interpret remaining bits as a normal unsigned int (the magnitude) Disadvantages: Two zeros: -0 and +0 [1000 and 0000] Less useful for arithmetic because the sign bit has no relationship with the magnitude--cannot use unsigned arithmetic logic! 0 011 = 3 1 011 = -3 0 111 = 7 0 111 (7) 1 011 (-3)? 010

Caution: language technicalities Ones complement and two s complement are both an operation and an encoding e.g., perform two s complement vs the number is stored in two s complement The operation represents the action necessary to negate a number in that encoding. e.g., performing two s complement (ones complement and add one) negates a number in two s complement encoding If you have a value in a particular encoding: If the sign bit is not set, it s a regular positive number If it is set, perform the operation to recover the positive value

Ones complement Ones complement Invert all the bits (~ operator in C) to negate Still have two representations of zero (1111 and 0000) Also, less useful for arithmetic than two s complement However, there is a neat trick: to perform two s complement, just do ones complement then add one Ex: 5 = 0101 (one s comp.) 1010 (add one) 1011 = -5 (-8 + 2 + 1) Aside: Why does this work? The sum of a number x and its ones complement is all ones (or 2 N -1 where N is the number of bits), so its ones' complement can be expressed as 2 N -1 - x. Because taking the two s complement of x is equivalent to subtracting x from 2 N, if we add one to the ones' complement the results are equal: (2 N -1 - x) + 1 = 2 N - x

Two s complement encoding Two s complement Take ones complement then add one to negate Equivalently: subtract number from 2N where N is the number of bits Implication: half of all values as negative One more negative number than positive numbers Positive numbers wrap around to negative ones halfway through 2 s Comp. Unsigned -1 1111 15-7 1001 9-8 1000 8 7 0111 7 1 0001 1 0 0000 0 negative numbers

Comparison We ll see one more signed integer encoding next week: offset binary / biased / excess For now, here s a comparison (for 1-byte integers): Binary Unsigned Sign Mag Ones C Two s C Offset-127 1111 1111 255-127 -0-1 128 1111 1110 254-126 -1-2 127 1000 0001 129-1 -126-127 2 1000 0000 128-0 -127-128 1 0111 1111 127 127 127 127 0 0111 1110 126 126 126 126-1 0000 0001 1 1 1 1-126 0000 0000 0 0 0 0-127

Comparison negative numbers Binary Unsigned Sign Mag Ones C Two s C Offset-127 1111 1111 255-127 -0-1 128 1111 1110 254-126 -1-2 127 1000 0001 129-1 -126-127 2 1000 0000 128-0 -127-128 1 0111 1111 127 127 127 127 0 0111 1110 126 126 126 126-1 0000 0001 1 1 1 1-126 0000 0000 0 0 0 0-127

Two s complement encoding Alternate interpretation: value of most significant bit is negated i.e., start at most negative number and build back up towards zero

Two s complement encoding Two s complement advantage: uses unsigned arithmetic logic (ignore carries out of the sign bit for now) Ex: 5 3 = 5 + (-3) = 0101 + 1101 = 0010 (2) Ex: 1 3 = 1 + (-3) = 0001 + 1101 = 1110 (-2) Ex: -2 3 = (-2) + (-3) = 1110 + 1101 = 1011 (-5) 0011 = 3 1100 1101 = -3 0111 (7) 1101 (-3) 0100 (4) 0111 = 7

Integer representations Information = Bits + Context What does 1011 mean? It depends! Unsigned: 11 Sign magnitude: -3 Ones' complement: -4 Two's complement: -5

Conversions Smaller unsigned larger unsigned Safe; zero-extend to preserve value 0101 (5) 0000 0101 (5) Smaller two s comp. larger two s comp. Safe; sign-extend to preserve value Larger smaller (unsigned or two s comp.) 1101 (-3) 1111 1101 (-3) 0000 0101 (5) 0101 (5) 0011 0101 (53) 0101 (5) Overflow if new type isn t large enough to fit (truncate) Unsigned two s comp. Overflow if first bit is non-zero (otherwise, no change) 0101 (5) 0101 (5) 1101 (13) 1101 (-2) Two s comp. unsigned Overflow if value is negative (otherwise, no change) 0101 (5) 0101 (5) 1101 (-2) 1101 (13)