World Inside a Computer is Binary

Similar documents
COMP2611: Computer Organization. Data Representation

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Chapter 3: part 3 Binary Subtraction

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

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

Chapter 10 Binary Arithmetics

COMP Overview of Tutorial #2

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

Number representations

CS & IT Conversions. Magnitude 10,000 1,

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

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Numeric Encodings Prof. James L. Frankel Harvard University

Part 2,Number Systems Questions

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

Number System. Introduction. Decimal Numbers

MACHINE LEVEL REPRESENTATION OF DATA

CS101 Lecture 04: Binary Arithmetic

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

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

CS 261 Fall Mike Lam, Professor Integer Encodings

Chapter 1 Review of Number Systems


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

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

Representation of Non Negative Integers

Chapter 4 Arithmetic Functions

Number Systems CHAPTER Positional Number Systems

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

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Numbering systems. Dr Abu Arqoub

CS 101: Computer Programming and Utilization

Arithmetic Processing

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

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

Binary Codes. Dr. Mudathir A. Fagiri

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

CMPE223/CMSE222 Digital Logic Design. Positional representation

Signed umbers. Sign/Magnitude otation

M1 Computers and Data

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

1.1. Unit 1. Integer Representation

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

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

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

MC1601 Computer Organization

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

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

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

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

Chapter 4: Data Representations

Module 2: Computer Arithmetic

CHW 261: Logic Design

Number Systems. Both numbers are positive

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

Chapter 2: Number Systems

Chapter 1. Digital Systems and Binary Numbers

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

Numerical Representations On The Computer: Negative And Rational Numbers

Number Systems Base r

EE 109 Unit 19. IEEE 754 Floating Point Representation Floating Point Arithmetic

a- As a special case, if there is only one symbol, no bits are required to specify it.

Numerical Representations On The Computer: Negative And Rational Numbers

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

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

Number Systems (2.1.1)

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

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

IEEE 754 Floating-Point Format

Korea University of Technology and Education

Chapter 2 Bits, Data Types, and Operations

Advanced Computer Architecture-CS501

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

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

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

Computer Arithmetic. L. Liu Department of Computer Science, ETH Zürich Fall semester, Reconfigurable Computing Systems ( L) Fall 2012

Chapter 3: Arithmetic for Computers

carry in carry 1101 carry carry

Floating Point Numbers. Lecture 9 CAP

Floating Point Arithmetic

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

Chapter Three. Arithmetic

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

Chapter 5 : Computer Arithmetic

Computer Organization

in this web service Cambridge University Press

Final Labs and Tutors

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.

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

Digital Fundamentals

C-string format with scanf/printf

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Chapter 2 Bits, Data Types, and Operations

Introduction to Numbering Systems

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

Computer Sc. & IT. Digital Logic. Computer Sciencee & Information Technology. 20 Rank under AIR 100. Postal Correspondence

UNIT-III COMPUTER ARTHIMETIC

Programming Studio #1 ECE 190

Floating Point January 24, 2008

Transcription:

C Programming 1 Representation of int data World Inside a Computer is Binary

C Programming 2 Decimal Number System Basic symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Radix-10 positional number system. The radix is also called the base of the number system. 12304 = 1 10 4 +2 10 3 +3 10 2 +0 10 1 +4 10 0

C Programming 3 Unsigned Binary Number System Basic symbols: 0, 1 Radix-2 positional number system. 10110 = 1 2 4 +0 2 3 +1 2 2 +1 2 1 +0 2 0 The value is 22 in decimal.

C Programming 4 Decimal to Binary Conversion

C Programming 5 Remainders 2 89 2 44 1 lsb 2 22 0 2 11 0 2 5 1 2 2 1 2 1 0 0 1 msb 1 0 1 1 0 0 1

C Programming 6 Decimal to Binary Conversion 89 D = 1 2 6 +0 2 5 +1 2 4 +1 2 3 +0 2 2 +0 2 1 +1 2 0 = 2 (2 (2 (2 (2 (2 1+0)+1)+1)+0)+0)+1

C Programming 7 Finite word Size of the CPU 32-bit word: 31 30 0 Largest Number: 31 i=0 2i = 4294967295, Smallest Number: 0 31 30 0 1 1 1 31 30 0 0 0 0

C Programming 8 Note The range of unsigned int or unsigned is (in 32-bits) 0 to 4294967295. The range of unsigned short int or unsigned short is (in 16-bits) 0 to 65535.

C Programming 9 An Example with 4-bit Word Size b 3 b 2 b 1 b 0 The Range of unsigned integer is: 0 to 2 4 1 = 15

C Programming 10 Bit String Decimal b 3 b 2 b 1 b 0 Value 0 0 0 0 0 0 0 0 1 1 0 0 1 0 2 0 0 1 1 3 0 1 0 0 4 0 1 0 1 5 0 1 1 0 6 0 1 1 1 7

C Programming 11 Bit String Decimal b 3 b 2 b 1 b 0 Value 1 0 0 0 8 1 0 0 1 9 1 0 1 0 10 1 0 1 1 11 1 1 0 0 12 1 1 0 1 13 1 1 1 0 14 1 1 1 1 15

C Programming 12 Signed Decimal Number We use + and symbols to indicate sign of a decimal number. In a binary system only two symbols {0,1} are available to encode any information. So one extra bit is required to indicate the sign of a number.

C Programming 13 Three Popular Schemes Signed Magnitude, 1 s Complement, 2 s Complement

C Programming 14 Signed Magnitude Consider a 4-bit word as an example. b 3 b 2 b 1 b 0 b 3 is zero (0) for a positive number and it is one (1) for a negative number. Other three bits b 2 b 1 b 0 represent the magnitude of the number.

C Programming 15 Bit String Decimal b 3 b 2 b 1 b 0 Value 0 0 0 0 +0 0 0 0 1 +1 0 0 1 0 +2 0 0 1 1 +3 0 1 0 0 +4 0 1 0 1 +5 0 1 1 0 +6 0 1 1 1 +7

C Programming 16 Bit String Decimal b 3 b 2 b 1 b 0 Value 1 1 1 1-7 1 1 1 0-6 1 1 0 1-5 1 1 0 0-4 1 0 1 1-3 1 0 1 0-2 1 0 0 1-1 1 0 0 0-0

C Programming 17 Signed magnitude There are two representations of zero: +0, 0, The range is [ 7,,+7] in 4-bits. If the word size is n-bits, the range is [ (2 n 1 1),,+(2 n 1 1)].

C Programming 18 1 s Complement Numeral Positive Numbers are same as the signed magnitude representation with the most significant bit a zero. If n is a number in 1 s complement form, n is obtained by changing every bit to its complement. The result is called the 1 s complement of n. a b 3 in case of 4-bits and b n 1 in case of n-bits.

C Programming 19 1 s Complement Numeral A negative number has one (1) in its most significant bit. +109 {}}{ 0 1 1 0 1 1 0 1 1 s complement 109 {}}{ 1 0 0 1 0 0 1 0 109 {}}{ 1 0 0 1 0 0 1 0 1 s complement +109 {}}{ 0 1 1 0 1 1 0 1

C Programming 20 Decimal Bit String Bit String Decimal Value b 3 b 2 b 1 b 0 b 3 b 2 b 1 b 0 Value +7 0 1 1 1 1 0 0 0 7 +6 0 1 1 0 1 0 0 1 6 +5 0 1 0 1 1 0 1 0 5 +4 0 1 0 0 1 0 1 1 4 +3 0 0 1 1 1 1 0 0 3 +2 0 0 1 0 1 1 0 1 2 +1 0 0 0 1 1 1 1 0 1 +0 0 0 0 0 1 1 1 1 0

C Programming 21 1 s Complement Representation Two representations of zero: +0, 0. The range is [ 7,,+7] in 4-bits. The range is [ (2 n 1 1),,+(2 n 1 1)]. Positive number representation is identical to signed magnitude, but the negative number representations are different.

C Programming 22 Signed Magnitude Verses 1 s Complement Decimal Signed Magnitude 1 s Complement 0 1000 1111 1 1001 1110 2 1010 1101 3 1011 1100 4 1100 1011 5 1101 1010 6 1110 1001 7 1111 1000

C Programming 23 2 s Complement Positive Numbers are same as the signed magnitude and 1 s complement representations with the most significant bit a zero. If n is a number in 2 s complement form, n is obtained by changing every bit to its complement and finally adding one to the complemented number. a b 3 in case of 4-bits and b n 1 in case of n-bits.

C Programming 24 2 s Complement Numeral A negative number has a one (1) in the most significant position. +109 {}}{ 0 1 1 0 1 1 0 1 109 {}}{ 1 0 0 1 0 0 1 1 1 s complement 10010010 + 1 109 2 s complement {}}{ 1 0 0 1 0 0 1 1 1 s complement 01101100 + 1 +109 2 s complement {}}{ 0 1 1 0 1 1 0 1

C Programming 25 Direct 2 s Complement 1 0 1 1 0 1 0 11 0 0 0 0 2 s Comp 0 1 0 0 1 0 1 01 0 0 0 0

C Programming 26 2 s Complement Numeral Only one representations of zero: 0000. The range is [ 8,,+7] for 4-bits. For n-bits, the range is i[ (2 n 1 ),,+(2 n 1 1)]. Positive representation is identical to signed magnitude and 1 s complement, but the negative representation is different.

C Programming 27 int, short int 1. The range of data of type int (32-bits) is -2147483648 to 2147483647. 2. The range of short int (16-bits) is -32768 to 32767. 3. The range of long long int (64-bits) is -9223372036854775808 to 9223372036854775807.

C Programming 28 Decimal Bit String Bit String Decimal Value b 3 b 2 b 1 b 0 b 3 b 2 b 1 b 0 Value +7 0 1 1 1 1 0 0 0 8 +6 0 1 1 0 1 0 0 1 7 +5 0 1 0 1 1 0 1 0 6 +4 0 1 0 0 1 0 1 1 5 +3 0 0 1 1 1 1 0 0 4 +2 0 0 1 0 1 1 0 1 3 +1 0 0 0 1 1 1 1 0 2 0 0 0 0 0 1 1 1 1 1

C Programming 29 Signed Magnitude, 1 s and 2 s Complements

C Programming 30 Decimal Sig. Mag. 1 s Compl. 2 s Compl. 0 1000 1111 1 1001 1110 1111 2 1010 1101 1110 3 1011 1100 1101 4 1100 1011 1100 5 1101 1010 1011 6 1110 1001 1010 7 1111 1000 1001 8 1000

C Programming 31 Interpretation of Bits Consider a 8-bit 2 s complement number. If the number is positive e.g. 01101010, the value is as usual 2 6 +2 5 +2 3 +2 1 = 64+32+8+2 = 106 (decimal).

C Programming 32 Interpretation of Bits If the number is negative e.g. 11101010, its value is (10000 0000 1110 1010) i.e. [2 8 (2 7 +2 6 +2 5 +2 3 +2 1 )] = 2 7 +2 6 +2 5 +2 3 +2 1 = 22(in decimal). Weight of msb is ve

C Programming 33 Sign Bit Extension 39 1011001 11011001 111011001 1111011001 11111011001 111111011001 25 0011001 00011001 000011001 0000011001 00000011001 000000011001

C Programming 34 2 s Complement Addition 0011 3 + 0010 + 2 0101 5 0011 3 + 1011 + 5 1110 2 1101 3 + 1110 + 2 1 1011 5 0100 4 + 0101 + 5 1001 7 Overflow

C Programming 35 2 s Complement Addition 1101 3 + 1010 + 6 1 0111 7 Overflow There may be carry-out without overflow. There may not be overflow even if there is carry-out.

C Programming 36 int in Your Machine The C data type int is a 32-bit 2 s complement number. Its range is 2147483648 to +2147483647. If one (1) is added to the largest positive number, the result is the smallest negative number. 0111 1111 1111 1111 1111 1111 1111 1111 2147483647 +1 1000 0000 0000 0000 0000 0000 0000 0000 2147483648

C Programming 37 10 s Complement Number The 2 s complement numeral is nothing special. We can use radix-complement numerals for any radix to represent signed numbers without importing any new sign symbol. We consider radix-complement decimal or 10 s complement numerals.

C Programming 38 3-digit 10 s Complement Numeral There are one thousand patterns (000 to 999) with three decimal digits, we interpret them in the following way: If the most significant digit is any one of {0,1,2,3,4}, the denotation is a usual positive number e.g. 341 is same as usual decimal 341.

C Programming 39 3-digit 10 s Complement Numeral If the most significant digit is any one of {5,6,7,8,9}, the number is treated as a negative number (n). If n is a 10 s complement number, n is obtained by ordinary decimal subtraction 1000 n.

C Programming 40 10 s Complement Numeral Consider the 3-digit 10 s complement numeral 725. It is a negative number whose magnitude is 1000 725 = 275. The range of numbers represented in 3 digits is 10 3 /2 to +10 3 /2 1. In n-digits the range is 10 n /2 to +10 n /2 1.

C Programming 41 Addition of 10 s Complement Numeral 127 + 205 332 427 + 305 732 127 + 821 ( 179) 948 ( 52) overflow 821 ( 179) + 753 ( 247) 1 574 ( 426) 521 ( 179) + 753 ( 247) 1 274

C Programming 42 Digit Extension 3-digit 4-digit 5-digit Decimal value 234 0234 00234 234 721 9721 99721 279