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

Similar documents
The Design of C: A Rational Reconstruction

The Design of C: A Rational Reconstruction"

The Design of C: A Rational Reconstruction" Jennifer Rexford!

The Design of C: A Rational Reconstruction

Fundamentals of Programming Session 2

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

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

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

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

Korea University of Technology and Education

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

COMP Overview of Tutorial #2

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

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

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

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

More about Binary 9/6/2016

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

CS 253. January 14, 2017

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

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

Number Systems and Number Representation

MC1601 Computer Organization

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

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

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

Beginning C Programming for Engineers

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

Chapter 10 Binary Arithmetics

DIGITAL SYSTEM DESIGN

Data Representation 1

Chapter 1. Digital Systems and Binary Numbers

EE 109 Unit 6 Binary Arithmetic

Number representations

Topic Notes: Bits and Bytes and Numbers

Fundamentals of Programming (C)

Chapter 3: part 3 Binary Subtraction

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

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

Chapter 2 Bits, Data Types, and Operations

Applied Computer Programming

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

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

Topic Notes: Bits and Bytes and Numbers

Integer Representation

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Arithmetic Operations

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

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

Chapter 2 Bits, Data Types, and Operations

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

Inf2C - Computer Systems Lecture 2 Data Representation

Binary Representations and Arithmetic

Lecture 13 Bit Operations

CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

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

Final Labs and Tutors

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

COMP2611: Computer Organization. Data Representation

Fundamentals of Programming

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

Arithmetic and Bitwise Operations on Binary Data

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Number System. Introduction. Decimal Numbers

COE 202: Digital Logic Design Number Systems Part 2. Dr. Ahmad Almulhem ahmadsm AT kfupm Phone: Office:

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

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

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

CS 121 Digital Logic Design. Chapter 1. Teacher Assistant. Hadeel Al-Ateeq

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

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

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Chapter 2 Bits, Data Types, and Operations

Representing and Manipulating Integers Part I

Chapter 4. Operations on Data

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

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.

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

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

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

Chapter 2 Bits, Data Types, and Operations

LOGIC DESIGN. Dr. Mahmoud Abo_elfetouh

Chapter 2: Number Systems

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Digital Systems and Binary Numbers

Binary Values. CSE 410 Lecture 02

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-2700: Digital Logic Design Winter Notes - Unit 4. hundreds.

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-278: Digital Logic Design Fall Notes - Unit 4. hundreds.

D I G I T A L C I R C U I T S E E

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

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

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

LAB A Translating Data to Binary

SIGNED AND UNSIGNED SYSTEMS

Chapter 2 Bits, Data Types, and Operations

Representing Information. Bit Juggling. - Representing information using bits - Number representations. - Some other bits - Chapters 1 and 2.3,2.

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

Logic Circuits I ECE 1411 Thursday 4:45pm-7:20pm. Nathan Pihlstrom.

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

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

Transcription:

CSC 2400: Computer Systems Bits, Bytes and Data Types 1 Goals for this Week Binary number system Why binary? Converting between decimal and binary and octal and hexadecimal number systems Finite representations of binary integers Unsigned and signed integers Integer addition and subtraction Bitwise operators AND, OR, NOT, and XOR Shift-left and shift-right 2

Analog/Analogue Systems Analogue Systems V(t) can have any value between its minimum and maximum value V(t) Digital Systems Digital Systems V(t) takes a value selected from a set of values Binary digital systems form the basis of almost all hardware systems currently V(t) 1 0 1 0 1

Why Bits (Binary Digits)? Computers are built using digital circuits Inputs and outputs can have only two values True (high voltage) or false (low voltage) Represented as 1 and 0 Can represent many kinds of information Boolean (true or false) Numbers (23, 79, ) Characters ( a, z, ) Pixels, sounds Internet addresses Can manipulate in many ways Read and write Logical operations Arithmetic 5 Coding A single binary input can have two values: 1 or 0 More bits = more combinations 0 0 0 1 1 0 1 1 How many values can you represent on 3 bits? What about n bits? 6

Memorize Powers of 2 Powers of 2 2 3 = 8 2 4 = 16 2 5 = 32 2 6 = 64 2 7 = 128 2 8 = 256 2 9 = 512 Powers of 10 2 10 = 1Kilo (1024) 10 3 = 1,000 2 20 = 1Mega (1024 2 = 1,048,576 ) 10 6 = 1,000,000 2 30 = 1Giga (1024 3 = 1,073,741,824) 10 9 = 1,000,000,000 2 40 = 1Tera (1024 4 = 1,099,511,627,776) 10 12 = 1,000,000,000,000 7 Base 10 and Base 2 Decimal (base 10) Each digit represents a power of 10 4173 10 = 4 x 10 3 + 1 x 10 2 + 7 x 10 1 + 3 x 10 0 Binary (base 2) Each bit represents a power of 2 1010 2 = 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 0 x 2 0 = 10 10 8

Binary to Decimal Sum up the place values of all 1-bits: 256 128 64 32 16 8 4 2 1 1 0 0 0 0 0 1 1 1 0 1 0 0 1 1 0 0 Place Value 10000011 2 = 10? 101001100 2 = 10? 9 Decimal to Binary Use the Placement Method Powers of 2 1024 512 256 128 64 32 16 8 4 2 1 155 10 = 2? 128 goes into 155 once leaving 27 to be placed 1??????? 64 and 32 are too big (make them 0) 16 goes in once leaving 11 1 0 0 1???? and so on 10

You Try It 11 Working w/ Bits is Tedious for People Express large binary numbers in hex using ¼ fewer digits: Hex Binary Decimal 0 0000 0 1 0001 1 2 0010 2 3 0011 3 4 0100 4 5 0101 5 6 0110 6 7 0111 7 Hex Binary Decimal 8 1000 8 9 1001 9 A 1010 10 B 1011 11 C 1100 12 D 1101 13 E 1110 14 F 1111 15 This is the hexadecimal system. Memorize the tables above. 12

Exercises 1. Based on the lookup tables on the previous page, convert the hex value 0x7A8BF7D6 into its binary equivalent: 7 A 8 B F 7 D 6 0111 1010 1000 1011 1111 0111 1101 0110 2. Based on the lookup tables on the previous page, convert the binary 10 0110 1110 1001 0100 1100 0101 1111 2 to hex: 0010 0110 1110 1001 0100 1100 0101 1111 2 6 E 9 4 B 5 F Why hexadecimal? Widely used in assembly language programming Used in network programming and debugging Used in graphic design programs e.g., for the red, green and blue components of a color: FF0000 represents red, for example. How many bits are used to represent each color? How many different colors can be represented?

Decimal Addition From right to left, we add each pair of digits We write the sum, and add the carry to the next column 1 9 8 + 2 6 4 Sum Carry 4 6 2 0 1 1 15 Hex Addition Hex addition is similar to decimal addition except that each hex digit has a range of 0 to F instead of 0 to 9, and a carry out occurs when the sum of hex digits in a particular column exceeds F (15 decimal). Examples: 1 C 3 C 7 8 5 + 2 7 + 2 A + E D E A F 1 1 7 2 1 1 1 3 B 9 A + D 2 E 6 1 0 E 8 0

Hex Subtraction Hex subtraction is similar to decimal subtraction except that if the subtrahend is greater than the minuend, we must borrow 16 (10 hex) from the previous digit. Examples: C A - 2 5 A 5 A 1 / B 3-3 6 7 D C 1 2 / / 1 D 3 5 F - 4 3 B 6 8 F A 9 Recall: Decimal Addition From right to left, we add each pair of digits We write the sum, and add the carry to the next column Base 10 Base 2 1 9 8 + 2 6 4 Sum Carry 4 6 2 0 1 1 0 1 1 + 0 0 1 Sum Carry 18

Finite Representation of Integers Fixed number of bits in memory Usually 8, 16, or 32 bits (1, 2, or 4 bytes) Unsigned integer No sign bit Always 0 or a positive number All arithmetic is modulo 2 n Examples of unsigned integers 00000001 1 00001111 00010000 00100001 11111111 20 Modulo Arithmetic Consider only numbers in a range E.g., five-digit car odometer: 0, 1,, 99999 E.g., eight-bit numbers 0, 1,, 255 Roll-over when you run out of space E.g., car odometer goes from 99999 to 0, 1, E.g., eight-bit number goes from 255 to 0, 1, Adding 2 n doesn t change the answer For eight-bit number, n=8 and 2 n =256 E.g., (37 + 256) mod 256 is This can help us do subtraction Suppose you want to compute a b Note that this equals a + (256-1 - b) + 1 21

One s and Two s Complement One s complement: flip every bit E.g., b is 01000101 (i.e., 69 in decimal) One s complement is That s simply 255-69 Subtracting from 11111111 is easy (no carry needed!) 1111 1111-0100 0101 b one s complement Two s complement Add 1 to the one s complement E.g., (255 69) + 1 22 Putting it All Together Computing a b Same as a + 256 b Same as a + (255 b) + 1 Same as a + onescomplement(b) + 1 Same as a + twoscomplement(b) Example: 172 69 The original number 69: One s complement of 69: Two s complement of 69: Add to the number 172: The sum comes to: Equals: 103 in decimal 23

Reading Two s Complement Patterns When a two s complement number has a highest bit 1, it indicates that the number is negative. To find the value, perform the same steps: Unknown value: 1011 1011 One s complement: Add 1 (two s complement): We get a value of 69, so the original pattern must have been -69. Practice: Ch. 2, Exercise 2 24 Signed Integers Sign-magnitude representation Use one bit to store the sign Zero for positive number One for negative number Examples E.g., 0010 1100 44 1010 1100-44 Hard to do arithmetic this way, so it is rarely used Complement representation One s complement Flip every bit E.g., 1101 0011-44 Two s complement Flip every bit, then add 1 E.g., 1101 0100-44 25

Fill in the Table Bit Pattern Value (Sign Magnitude) Value (One s Complement) Value (Two s Complement) 000 001 010 011 100 101 110 111 26 Question What value does 10011001 represent? [Ex. 6, page 69] 27

Overflow: Running Out of Room Adding two large integers together Sum might be too large to store in the number of bits available What happens? Unsigned integers All arithmetic is modulo arithmetic Sum would just wrap around Signed integers Can get nonsense values Example with 16-bit integers Sum: 10000+20000+30000 Result: -5536 28 Exercise Assume only four bits are available for representing integers, and signed integers are represented in 2 s complement. Compute the value of the expression 7 + 7 29

Bitwise Operations 30 Networking 31

Encryption/ Decryption 32 Compression 33

Bitwise Operators: AND and OR Bitwise AND (&) Bitwise OR ( ) & 0 0 1 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 & 15 0 0 0 0 1 1 1 1 15 0 0 0 0 1 1 1 1 34 Bitwise Operators: Not and XOR One s complement (~) Turns 0 to 1, and 1 to 0 E.g., set last three bits to 0 x = x & XOR (^) 0 if both bits are the same 1 if the two bits are different ^ 0 1 0 1 Practice: Ch. 2, Exercise 9 35

Bitwise Operators: Shift Left/Right Shift left (<<): Multiply by powers of 2 Shift some # of bits to the left, filling the blanks with 0 53 0 0 1 1 0 1 0 1 53<<2 Shift right (>>): Divide by powers of 2 Shift some # of bits to the right. Fill in blanks with sign bit. 53 0 0 1 1 0 1 0 1-75 1 0 1 1 0 1 0 1 53>>2-75>>2 sign extension sign extension Practice: Ch. 2, Exercises 10, 12, 13, 14 36 Bitmasks Used to change or query one or more bits in a variable. The bitmask indicates which bits are to be affected. Common operations: Operation Expression C expression Set N th bit of x x = x OR 2 N x = x (1 << N); Clear N th bit of x x = x x = ; Read N th bit of x??? = x??? = x ; Can extend to groups of bits 37

Exercise Let x be an 8-bit integer. Set the 5 th least significant bit of x: Clear the 5 th least significant bit of x: 38 Bitmask Example /* This program demonstrates setting a bit, clearing a bit, and ** reading a bit. (pg 64) */ #include <stdio.h> main() { char a; int i; a=17; a=a (1 << 3); /* set 3rd bit */ printf("%d\n",a); a=a & (~(1<<4)); /* clear 4th bit */ printf("%d\n",a); } for (i=7; i>=0; i--) printf("%d ",(a&(1<<i)) >> i); /* read i'th bit */ printf("\n"); 39

Exercise #1 Write a small program that reads in an integer and prints out its binary representation. Sample output: Please enter an integer: 1025 The binary representation of 1025 is 00000000 00000000 00000100 00000001 40 Exercise #2 Write a small program that reads in an integer prints out hexadecimal value of its bytes, separated by spaces, starting with the most significant byte. Sample output: Please enter an integer: 1025 The four bytes (in hexadecimal) are: 00 00 04 01 42

Example: Counting the 1 s How many 1 bits in a number? E.g., how many 1 bits in the binary representation of 53? Four 1 bits How to count them? 0 0 1 1 0 1 0 1 44 Counting the Number of 1 Bits #include <stdio.h> #include <stdlib.h> int main(void) { unsigned n, count; printf( Number: "); if (scanf("%u", &n)!= 1) { fprintf(stderr, "Error: Expect number.\n"); exit(exit_failure); } /* Enter code to count the 1 bits */ } printf( Number of 1 bits: %u\n, count); return 0; 45

Practice Write a function getbyte that extracts a byte from a word. /* * getbyte(x, n) - Extract byte n from word x * Bytes numbered from 0 (LSB) to 3 (MSB) * Examples: getbyte(0x12345678,1) = 0x56 * Legal operations: ~ & ^ + << >> */ int getbyte(int x, int n) { /* Add code here */ } 46 Summary Computer represents everything in binary Integers, floating-point numbers, characters, addresses, Pixels, sounds, colors, etc. Binary arithmetic through logic operations Sum (XOR) and Carry (AND) Two s complement for subtraction Binary operations in C AND, OR, NOT, XOR, shift left and shift right Useful for efficient and concise code, though sometimes cryptic 47

Required Reading Textbook, Chapter 2 48