Integer Representation

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

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

CS 64 Week 1 Lecture 1. Kyle Dewey

More about Binary 9/6/2016

Integers II. CSE 351 Autumn 2018

Arithmetic Operations

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Inf2C - Computer Systems Lecture 2 Data Representation

COMP2611: Computer Organization. Data Representation

Integer Representation

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

Computer Systems C S Cynthia Lee

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Arithmetic and Bitwise Operations on Binary Data

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

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

Number System. Introduction. Decimal Numbers

LAB A Translating Data to Binary

Arithmetic and Bitwise Operations on Binary Data

CS107, Lecture 2 Bits and Bytes; Integer Representations

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Operations, Operands, and Instructions

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

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

CS 261 Fall Mike Lam, Professor Integer Encodings

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

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

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

Chapter 3: part 3 Binary Subtraction

World Inside a Computer is Binary

INF2270 Spring Philipp Häfliger. Lecture 4: Signed Binaries and Arithmetic

Data III & Integers I

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

Chapter 10 Binary Arithmetics

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

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Computer Organization and Levels of Abstraction

Signed Binary Numbers

Beginning C Programming for Engineers

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

Data III & Integers I

Data III & Integers I

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

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,...

Review Activity 1 CALL and RET commands in assembler

CS 261 Fall Binary Information (convert to hex) Mike Lam, Professor

Chapter 4: Data Representations

Computer Organization & Systems Exam I Example Questions

CSCI 2212: Intermediate Programming / C Chapter 15

Number Systems and Computer Arithmetic

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

SIGNED AND UNSIGNED SYSTEMS

Bits, Bytes, and Integers

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

EE 109 Unit 6 Binary Arithmetic

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

Binary Representations and Arithmetic

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

Bitwise Data Manipulation. Bitwise operations More on integers

Chapter 4 Arithmetic Functions

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

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

Tutorial 1: C-Language

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

Bits, Bytes and Integers Part 1

Integer Encoding and Manipulation

Arithmetic Processing

EE292: Fundamentals of ECE

Bits, Bytes and Integers

Topic Notes: Bits and Bytes and Numbers

8/27/2016. ECE 120: Introduction to Computing. Graphical Illustration of Modular Arithmetic. Representations Must be Unambiguous

Bitwise Instructions

Survey. Motivation 29.5 / 40 class is required

CS 107 Lecture 2: Bits and Bytes (continued)

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

CSCI2467: Systems Programming Concepts

Unsigned Binary Integers

Unsigned Binary Integers

AGENDA Binary Operations CS 3330 Samira Khan

Computer Organization and Levels of Abstraction

Topic Notes: Bits and Bytes and Numbers

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Numerical Representations On The Computer: Negative And Rational Numbers

Binary Values. CSE 410 Lecture 02

Number Systems and Number Representation

CPS 104 Computer Organization and Programming

COMP 122/L Lecture 2. Kyle Dewey

Bits, Bytes, and Integers Part 2

C-string format with scanf/printf

Fundamentals of Programming Session 2

Numerical Representations On The Computer: Negative And Rational Numbers

Applied Computer Programming

Transcription:

Integer Representation

Announcements assign0 due tonight assign1 out tomorrow Labs start this week SCPD Note on ofce hours on Piazza Will get an email tonight about labs

Goals for Today Introduction to computer arithmetic Binary addition and subtraction Overfow Represent negative numbers Compare/contrast unsigned and signed integers

Add two 1-bit numbers a b a + b 0 0 00 0 1 01 1 0 01 1 1 10

Add two 1-bit numbers a b a + b 0 0 00 0 1 01 1 0 01 1 1 10 LSB looks like XOR, next bit looks like AND a + b = ((a & b) << 1) (a ^ b) (Only when a and b are 1-bit)

Add two 1-bit numbers a b a + b 0 0 00 0 1 01 1 0 01 1 1 10 LSB looks like XOR, next bit looks like AND a + b = ((a & b) << 1) (a ^ b) (Only when a and b are 1-bit) This is called a half adder "Full" addition requires us to handle carries

Addition in Binary 1 107 + 58 165

Addition in Binary 1 107 0110 1011 + 58 + 0011 1010 165

Addition in Binary 1 107 0110 1011 + 58 + 0011 1010 165 1

Addition in Binary 1 1 107 0110 1011 + 58 + 0011 1010 165 01

Addition in Binary 1 1 1 107 0110 1011 + 58 + 0011 1010 165 0101

Addition in Binary 1 1111 1 107 0110 1011 + 58 + 0011 1010 165 1010 0101 Same as decimal addition, just a lot more carrying

Overfow 255 1111 1111 + 1 + 0000 0001 What happens when we run out of bits?

Overfow 1 255 1111 1111 + 1 + 0000 0001 0 What happens when we run out of bits?

Overfow 1 1111 111 255 1111 1111 + 1 + 0000 0001 1 0000 0000 What happens when we run out of bits?

Overfow 1111 111 255 1111 1111 + 1 + 0000 0001 0 0000 0000 What happens when we run out of bits? Overfow: extra bit is dropped No warning, no indication

Overfow 1111 111 255 1111 1111 + 1 + 0000 0001 0 0000 0000 Modular arithmetic Overfow leads to wrapping around, mod by 256 0x100 % 256 = (1 0000 0000) % 256 = 0 256 = 2 8

unsigned char Number Circle 00000000 11111111 00000001 11111110 255 0 1 00000010 254 2 11000000 192 64 01000000 10000001 129 128 127 01111111 10000000

unsigned char Number Circle 00000000 11111111 00000001 11111110 255 0 1 00000010 254 2 Discontinuity 11000000 192 64 01000000 10000001 129 128 127 01111111 10000000

What about subtraction? Could use same approach as addition Start with 1-bit numbers, then deal with borrowing

What about subtraction? Could use same approach as addition Start with 1-bit numbers, then deal with borrowing But we don't like inventing new hardware Idea: Subtraction is just adding the negative a - b becomes a + (-b) New problem: How do we represent negatives?

Deriving -1 1 + -1 0

Deriving -1 1 0000 0001 + -1 +???????? 0 0000 0000 Let's backsolve for -1 We know that 1 + (-1) = 0

Deriving -1 1 1 0000 0001 + -1 +???????1 0 0000 0000

Deriving -1 11 1 0000 0001 + -1 +??????11 0 0000 0000

Deriving -1 1 1111 111 1 0000 0001 + -1 + 1111 1111 0 0000 0000

Deriving -1 1 1111 111 1 0000 0001 + -1 + 1111 1111 0 1 0000 0000 Did it...work? What about that extra 1?

Deriving -1 1111 111 1 0000 0001 + -1 + 1111 1111 0 0000 0000 Did it...work? What about that extra 1? Arithmetic is % 256, so it's dropped

What have we learned? For 1-byte numbers -1 is 0xff = 1111 1111 (bin) Note: same bit pattern as 255 in unsigned 0 = 0x100 % 256 = 1 0000 0000 (bin) % 256 But how do we generalize for other negatives?

Arithmetic Negation Let's try something diferent... Let n be a 1-byte number -n = 0 - n

Arithmetic Negation Let's try something diferent... Let n be a 1-byte number -n = 0 - n = 0x100 - n Reminder: 0x100 = 256 = 1 0000 0000 (bin)

Arithmetic Negation Let's try something diferent... Let n be a 1-byte number -n = 0 - n = 0x100 - n = (0xff + 1) - n Reminder: 0xff = 255 = 1111 1111 (bin)

Arithmetic Negation Let's try something diferent... Let n be a 1-byte number -n = 0 - n = 0x100 - n = (0xff + 1) - n = (0xff - n) + 1

Arithmetic Negation Let's try something diferent... Let n be a 1-byte number -n = 0 - n = 0x100 - n = (0xff + 1) - n = (0xff - n) + 1 Wouldn't it be awesome if there were a simple way to get 0xff - n?

0xff - n 0xff 1111 1111-0x35-0011 0101

0xff - n 0xff 1111 1111-0x35-0011 0101 10

0xff - n 0xff 1111 1111-0x35-0011 0101 0xca 1100 1010 There's always a 1 to take, never need to borrow

0xff - n 0xff 1111 1111-0x35-0011 0101 0xca 1100 1010 0xff - n = ~n For 1-byte n

Arithmetic Negation Let's try something diferent... Let n be a 1-byte number -n = 0 - n = 0x100 - n = (0xff + 1) - n = (0xff - n) + 1 Wouldn't it be awesome if there were a simple way to get 0xff - n?

Arithmetic Negation Let's try something diferent... Let n be a 1-byte number -n = 0 - n = 0x100 - n = (0xff + 1) - n = (0xff - n) + 1 -n = ~n + 1!! This is called 2's complement Works for all integer types, not just 1-byte

unsigned char Number Circle 00000000 11111111 00000001 11111110 255 0 1 00000010 254 2 11000000 192 64 01000000 10000001 129 128 127 01111111 10000000

00000000 11111111 00000001 11111110-1 0 1 00000010 11000000 01000000 10000001 10000000 01111111

00000000 11111111 00000001 11111110-1 0 1 00000010-2 2 11000000 01000000 10000001 10000000 01111111

00000000 11111111 00000001 11111110-1 0 1 00000010-2 2 11000000-64 64 01000000-127 127 10000001 01111111 10000000

signed char Number Circle 00000000 11111111 00000001 11111110-1 0 1 00000010-2 2 11000000-64 64 01000000 10000001-127 -128 127 01111111 10000000

signed char Number Circle 00000000 11111111 00000001 11111110-1 0 1 00000010-2 2 11000000-64 64 01000000 Discontinuity 10000001-127 -128 127 01111111 10000000

signed char -n = ~n + 1 Same 256 bit patterns Half the bit patterns used for negatives MSB = 1 means negative (sign bit) Discontinuity Diferent place than unsigned Asymmetry What is -(-128)?

(signed char) -(-128) -(-128) = -0x80 = ~0x80 + 1 = 0x7f + 1 = 0x80 = -128 -(1000 0000) ~(1000 0000) + 1 0111 1111 + 1 1000 0000-128 is its own arithmetic inverse

Larger Integer Types C integer types All but char default to signed (char's default is not defned) Data type sizes char: 1 byte, -128 to 127 short: 2 bytes, +/- 32k int: 4 bytes, +/- 2 billion long: 8 bytes, +/- [big number]

Code Examples

Signed vs. Unsigned What's the same? Most bitwise ops (&,, ^, ~, <<) Most arithmetic ops (+, -, *) What's diferent? Comparison (>, <) Right shift: arithmetic vs.logical Fill with sign bit vs. zero Assignment to larger type (e.g. assign short to int) Sign extend vs. zero extend Preserves value

Conclusions In the end, all operations manipulate bits Assignment, parameter passing just copy bits around Signed and unsigned are interpretations of bit patterns Bits aren't fundamentally one or the other Just like binary, decimal, hex are ways to read/write and think about bits

Summary Introduction to computer arithmetic Binary addition and subtraction Overfow Represent negative numbers Compare/contrast unsigned and signed integers This week's lab and assignment Practice with bits, bitwise ops, ints Next time: pointers and memory