Binary Representations and Arithmetic

Similar documents
CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

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

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

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

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

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

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

How a computer runs a program. Binary Representa8on and Opera8ons on Binary Data. Opera8ng System 9/17/13. You can view binary file contents

Arithmetic and Bitwise Operations on Binary Data

BINARY SYSTEM. Binary system is used in digital systems because it is:

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

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

Inf2C - Computer Systems Lecture 2 Data Representation

Number System. Introduction. Decimal Numbers

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

COMP Overview of Tutorial #2

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

T02 Tutorial Slides for Week 2

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

Chapter 2 Bits, Data Types, and Operations

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

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

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

Data Representation 1

Chapter 2 Bits, Data Types, and Operations

More about Binary 9/6/2016

CHW 261: Logic Design

Beginning C Programming for Engineers

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

Chapter 2 Number System

CS101 Lecture 04: Binary Arithmetic

CS61B Lecture #14: Integers

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

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

COMP2611: Computer Organization. Data Representation

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

Chapter 2 Bits, Data Types, and Operations

Representation of Non Negative Integers

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

TOPICS. Other Number Systems. Other Number Systems 9/9/2017. Octal Hexadecimal Number conversion

MACHINE LEVEL REPRESENTATION OF DATA

Number Systems and Number Representation

Advanced Computer Architecture-CS501

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

The Design of C: A Rational Reconstruction

Chapter 2 Bits, Data Types, and Operations

CS 261 Fall Mike Lam, Professor Integer Encodings

The Design of C: A Rational Reconstruction"

World Inside a Computer is Binary

Computer Organisation CS303

CS & IT Conversions. Magnitude 10,000 1,

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

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.

Bits, Words, and Integers

Fundamentals of Programming Session 2

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

Arithmetic Processing

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

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

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Number Representations

Introduction to Numbering Systems

Final Labs and Tutors

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

CS61c Midterm Review (fa06) Number representation and Floating points From your friendly reader

Signed Binary Numbers

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

EE 109 Unit 6 Binary Arithmetic

Chapter 7. Basic Types

Harry H. Porter, 2006

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

LING 388: Computers and Language. Lecture 5

CS/EE1012 INTRODUCTION TO COMPUTER ENGINEERING SPRING 2013 HOMEWORK I. Solve all homework and exam problems as shown in class and sample solutions

Number Systems. Readings: , Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

Binary Values. CSE 410 Lecture 02

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

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

UNIT 7A Data Representation: Numbers and Text. Digital Data

Introduction to Computer Science-103. Midterm

Digital Fundamentals

Number representations

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

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

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

COMP2121: Microprocessors and Interfacing. Number Systems

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

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

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

Signed umbers. Sign/Magnitude otation

Topic Notes: Bits and Bytes and Numbers

Arithmetic and Bitwise Operations on Binary Data

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Transcription:

Binary Representations and Arithmetic 9--26

Common number systems. Base : decimal Base 2: binary Base 6: hexadecimal (memory addresses) Base 8: octal (obsolete computer systems) Base 64 (email attachments, ssh keys)

Hexadecimal: Base 6 Indicated by prefacing number with x A number, written as the sequence of digits d n d n- d 2 d d where d is in {,,2,3,4,5,6,7,8,9,A,B,C,D,E,F}, represents the value d n * 6 n + d n- * 6 n- +... + d 2 * 6 2 + d * 6 + d * 6

What is the value of xb7 in decimal? A. 397 6 2 = 256 B. 49 C. 49 D. 437 E. 439

Each hex digit is a nibble Hex digit:6 values, 2 4 = 6 -> 4 bits / digit xb7 256s digit: 6s digit: B (decimal ) s digit: 7 In binary: B 7

Converting hex and binary A group of four binary digits maps to one hex digit. x 48C = b 4 à 8 à C à (2) à

What is b in hex? a) xb3b b) x59d c) xc5c d) x37b e) x5473

Converting among 2 x bases Amounts to re-grouping digits. Binary à octal: group sets of three digits Octal à hex: group pairs of digits Hex à base64: group sets of four digits Split digits into groups to reverse the conversion.

Converting among arbitrary bases The division-and-mod method always works. Requires division and mod in the start-base The subtract-base-powers method kind of works. Must modify to subtract multiples of powers of the base. Requires multiplication, powers and subtraction in the startbase. The sum-up-digits method always works. Requires multiplication, powers and addition in the end-base. We re used to thinking in base, so it often helps to use base as an intermediate step. I will only make you do conversions among bases 2,, and 6.

Unsigned Addition (4-bit) Addition works like grade school addition: 6 2 + + 4 + + 6 ^carry out Four bits give us range: - 5

Unsigned Addition (4-bit) Addition works like grade school addition: 6 2 + + 4 + + 6 ^carry out Four bits give us range: - 5 Overflow!

What s the sum? + a), carry out = b), carry out = c), carry out = d), carry out = e), carry out =

So far: Unsigned Integers With N bits, can represent values: to 2 n - We can always add s to the front of a number without changing it: = = = byte: char, unsigned char 2 bytes: short, unsigned short 4 bytes: int, unsigned int, float 8 bytes: long long, unsigned long long, double 4 or 8 bytes: long, unsigned long

Representing Signed Values One option (used for floats, NOT integers) Let the first bit represent the sign means positive means negative For example: -> 5 -> -5 Problem with this scheme?

Two s Complement The Encoding comes from Definition of the 2 s complement of a number: 2 s complement of an N bit number, x, is its complement with respect to 2 N Can use this to find the bit encoding, y, for the negation of x: For N bits, y = 2 N x X -X 2 4 - X 4 bit examples: = (only 4 bits) = = =

Two s Complement Only one value for zero With N bits, can represent the range: -2 N- to 2 N- First bit still designates positive () /negative () Negating a value is slightly more complicated: =, - = From now on, unless we explicitly say otherwise, we ll assume all integers are stored using two s complement! This is the standard!

Two s Compliment Each two s compliment number is now: -2 n- *d n- + 2 n-2 *d n-2 + + 2 *d + 2 *d Note the negative sign on just the first digit. This is why first digit tells us negative vs. positive.

2 s Complement to Decimal High order bit is the sign bit, otherwise just like unsigned conversion. 4-bit examples: : *-2 3 + *2 2 + *2 + *2 + 4 + 2 + = 6 : *-2 3 + *2 2 + *2 + *2-8 + 4 + 2 + = -2 Try:

What is in decimal? Each two s compliment number is now: -2 n- *d n- + 2 n-2 *d n-2 + + 2 *d + 2 *d A. -2 B. -7 C. -9 D. -25

Range of binary values Smallest unsigned value: = Largest unsigned value: = 2 N Smallest 2 s complement value: = -2 N- Largest 2 s complement value: = 2 N- -

Addition & Subtraction Addition is the same as for unsigned One exception: different rules for overflow + Can use the same hardware for both Subtraction is the same operation as addition Just need to negate the second operand 6-7 = 6 + (-7)

How to negate in 2 s complement. Flip the bits ( s become s, s become s) 2. Add Example: - *. Flip bits: 2. Add : + =

Subtraction Hardware Negate and add to second operand: Can use the same circuit for add and subtract: 6-7 == 6 + ~7 + input -------------------------------> input 2 --> possible bit flipper --> ADD CIRCUIT ---> result possible + input--------> Let s call this possible + input: Carry in (: on add, : on subtract)

Examples: 4 bit signed values ( a-b is a + ~b + ): subtraction: flip bits and add 3-6 = (6: ~6: ) + Addition: don t flip bits or add 3 + -6 = +

Convert and subtract Perform the subtraction 2 9 in 6-bit binary. 2 = 9 = ~9 = -9 = 2 9 = = -7

Bits and Bytes Bit: a or value (binary) HW represents as two different voltages : the presence of voltage (high voltage) : the absence of voltage (low voltage) Byte: 8 bits, the smallest addressable unit Memory: (address) [] [] [2] Other names: Nibble 4 bits: Word : Depends on system, often 4 bytes

How many unique values can we represent with 9 bits? One bit: two values ( or ) Two bits: four values (,,, or ) Three bits: eight values (,,,, ) A. 8 B. 8 C. 256 D. 52 E. Some other number of values.

How many values? bit: 2 bits: 3 bits: 4 bits: 6 values N bits: 2 N values

Determining sizes of C types (on my laptop) #include <stdio.h> int main(){ char c; short s; int i; long l; long long ll; float f; double d; } printf("size of char: %lu\n", sizeof(c)); printf("size of short: %lu\n", sizeof(s)); printf("size of int: %lu\n", sizeof(i)); printf("size of long: %lu\n", sizeof(l)); printf("size of long long: %lu\n", sizeof(ll)); printf("size of float: %lu\n", sizeof(f)); printf("size of double: %lu\n", sizeof(d));

Written homework # Will be released tomorrow. Will be due by 4:pm next Friday. Topics: binary/hex conversions binary arithmetic