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

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

Binary Representations and Arithmetic

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

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

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

MACHINE LEVEL REPRESENTATION OF DATA

COMP2611: Computer Organization. Data Representation

CS101 Lecture 04: Binary Arithmetic

More about Binary 9/6/2016

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

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

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

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

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

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

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

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

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

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

T02 Tutorial Slides for Week 2

Chapter 2 Bits, Data Types, and Operations

CHW 261: Logic Design

Objectives. Connecting with Computer Science 2

CS & IT Conversions. Magnitude 10,000 1,

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

Chapter 2 Bits, Data Types, and Operations

Numbering systems. Dr Abu Arqoub

Chapter 2 Bits, Data Types, and Operations

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Data Representation 1

Number System. Introduction. Decimal Numbers

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

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Level ISA3: Information Representation

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

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

Logic and Computer Design Fundamentals. Chapter 1 Digital Computers and Information

Unit 2: Data Storage CS 101, Fall 2018

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Electronic Data and Instructions

CMPE223/CMSE222 Digital Logic Design. Positional representation

Information Science 1

Chapter 2 Bits, Data Types, and Operations

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

Inf2C - Computer Systems Lecture 2 Data Representation

Module 1: Information Representation I -- Number Systems

data within a computer system are stored in one of 2 physical states (hence the use of binary digits)

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

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

Digital Computers and Machine Representation of Data

Machine Arithmetic 8/31/2007

BSM540 Basics of C Language

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

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

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

Digital Fundamentals

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Reserved Words and Identifiers

1 Digital tools. 1.1 Introduction

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

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Fundamentals of Programming (C)

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Final Labs and Tutors

Number Systems. Both numbers are positive

Chapter 2 Number System

EEM 232 Digital System I

Chapter 2 Binary Values and Number Systems

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

Chapter 3: Number Systems and Codes. Textbook: Petruzella, Frank D., Programmable Logic Controllers. McGraw Hill Companies Inc.

Moodle WILLINGDON COLLEGE SANGLI. ELECTRONICS (B. Sc.-I) Introduction to Number System

Computer System and programming in C

Lecture (01) Digital Systems and Binary Numbers By: Dr. Ahmed ElShafee

Signed Binary Numbers

Lecture 2: Number Systems

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those?

Chapter 4: Computer Codes. In this chapter you will learn about:

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

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

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

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

Binary Values. CSE 410 Lecture 02

Bits, Words, and Integers

Work relative to other classes

ECE232: Hardware Organization and Design

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

COE 202- Digital Logic. Number Systems II. Dr. Abdulaziz Y. Barnawi COE Department KFUPM. January 23, Abdulaziz Barnawi. COE 202 Logic Design

COMP2121: Microprocessors and Interfacing. Number Systems

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Binary Codes. Dr. Mudathir A. Fagiri

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

COMP Overview of Tutorial #2

User. Application program. Interfaces. Operating system. Hardware

CHAPTER 2 Data Representation in Computer Systems

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

ECE 2030B 1:00pm Computer Engineering Spring problems, 5 pages Exam Two 10 March 2010

Digital Systems COE 202. Digital Logic Design. Dr. Muhamed Mudawar King Fahd University of Petroleum and Minerals

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Engineering Computing I

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Transcription:

CS 3: Intro to Systems Binary Representation Kevin Webb Swarthmore College January 27, 25

Reading Quiz

Abstraction User / Programmer Wants low complexity Applications Specific functionality Software library Reusable functionality Operating system Manage resources Complex devices Compute & I/O

Today Number systems and conversion Data types and storage: Sizes Representation Signedness

You can view binary file contents xxd (or hexdump C) to view binary file values: xxd a.out # a binary executable file Address: value of the next 6 bytes in memory : 7f45 4c46 2 : 2 3e 37 4 2: 4 84d (these weird numbers (f,c,e, ), are hexidecimal digits ) xxd myprog.c # binary ascii encoding of C source: : 2369 6e63 6c75 6465 3c73 7464 696f 2e68 #i nc lu de <s td io.h : 3ea 696e 742 6d6 696e 2829 27b a2 >\n in t ma in () { \n

Data Storage Lots of technologies out there: Magnetic (hard drive, floppy disk) Optical (CD / DVD / Blu-Ray) Electronic (RAM, registers, ) Focus on electronic for now We ll see (and build) digital circuits soon Relatively easy to differentiate two states Voltage present Voltage absent

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: 4 bits: Nibble 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

C types and their (typical!) sizes 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 unsigned long v; short s; long long ll; printf( %lu %lu %lu\n, sizeof(v), sizeof(s), sizeof(ll)); // prints out number of bytes How do we use this storage space (bits) to represent a value?

Let s start with what we know Decimal number system (Base ) Sequence of digits in range [, 9] 6425 Digit #4 Digit #

What is the significance of the N th digit number in this number system? What does it contribute to the overall value? 6425 A. d N * B. d N * C. d N * N D. d N * N E. d N * d N Digit #4 d 4 Digit # d Consider the meaning of d 3 (the value 4) above. What is it contributing to the total value?

Positional Notation The meaning of a digit depends on its position in a number. A number, written as the sequence of digits d n d n- d 2 d d in base b represents the value d n * b n + d n- * b n- +... + d 2 * b 2 + d * b + d * b

Decimal: Base Used by humans 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}, represents the value d n * n + d n- * n- +... + d 2 * 2 + d * + d * 6425 = 6 * 4 + 4 * 3 + * 2 + 2 * + 5 * 6 + 4 + + 2 + 5

Binary: Base 2 Used by computers A number, written as the sequence of digits d n d n- d 2 d d where d is in {,}, represents the value d n * 2 n + d n- * 2 n- +... + d 2 * 2 2 + d * 2 + d * 2

What is the value of in decimal? A number, written as the sequence of digits d n d n- d 2 d d where d is in {,}, represents the value d n * 2 n + d n- * 2 n- +... + d 2 * 2 2 + d * 2 + d * 2 A. 26 B. 53 C. 6 D. 6 E. 28

Other (common) number systems. Base : decimal Base 2: binary Base 6: hexadecimal (memory addresses) Base 8: octal Base 64 (Commonly used on the Internet, e.g. email attachments).

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 B. 49 C. 49 D. 437 E. 439 6 2 = 256

Hexadecimal: Base 6 Indicated by prefacing number with x Like binary, base is power of 2 Fewer digits to represent same value Each digit is a nibble, or half a byte 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

Each hex digit is a nibble Hex digit:6 values, 2 4 = 6 -> 4 bits / digit x B 7 Four-bit value: Four-bit value: B (decimal ) Four-bit value: 7 In binary: B 7

Converting Decimal -> Binary Two methods: division by two remainder powers of two and subtraction

Method : decimal value D, binary result b (b i is ith digit): i = while (D > ) if D is odd Example: Converting 5 set b i to if D is even set b i to i++ D = D/2 idea: D = b example: D = 5 a = D = b D = 52 a = D/2 = b/2 D = 26 a2 = D/2 = b/2 D = 3 a3 = D/2 = b/2 D = 6 a4 = D/2 = b/2 D = 3 a5 = = D = a6 = D = a7 = 5 =

Method : decimal value D, binary result b (b i is ith digit): i = while (D > ) if D is odd Example: Converting 5 set b i to if D is even set b i to i++ D = D/2 idea: D = b example: D = 5 a = D/2 = b/2 D = 52 a = D/2 = b/2 D = 26 a2 = D/2 = b/2 D = 3 a3 = D/2 = b/2 D = 6 a4 = D/2 = b/2 D = 3 a5 = = D = a6 = D = a7 = 5 =

Method : decimal value D, binary result b (b i is ith digit): i = while (D > ) if D is odd Example: Converting 5 set b i to if D is even set b i to i++ D = D/2 idea: D = b example: D = 5 a = D/2 = b/2 D = 52 a = D/2 = b/2 D = 26 a2 = D/2 = b/2 D = 3 a3 = D/2 = b/2 D = 6 a4 = D/2 = b/2 D = 3 a5 = = D = a6 = D = a7 = 5 =

Method 2 2 =, 2 = 2, 2 2 = 4, 2 3 = 8, 2 4 = 6, 2 5 = 32, 2 6 = 64, 2 7 = 28 To convert 5: Find largest power of two that s less than 5 (64) Subtract 64 (5 64 = 4), put a in d 6 Subtract 32 (4 32 = 9), put a in d 5 Skip 6, it s larger than 9, put a in d 4 Subtract 8 (9 8 = ), put a in d 3 Skip 4 and 2, put a in d 2 and d Subtract ( = ), put a in d (Done)

What is the value of 357 in binary? A. B. C. D. E. 2 =, 2 = 2, 2 2 = 4, 2 3 = 8, 2 4 = 6, 2 5 = 32, 2 6 = 64, 2 7 = 28, 2 8 = 256

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?

Floating Point Representation bit for sign sign exponent fraction 8 bits for exponent 23 bits for precision value = (-) sign *.fraction * 2 (exponent-27) let's just plug in some values and try it out x4ac49ba: sign = exp = 29 fraction = 292458 = *.292458*2 2 = 5.698 I don t expect you to memorize this

Up Next: Binary Arithmetic