Professor Jennifer Rexford COS 217

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Advertisements

Number Bases Informatics INFO I101 February 9, 2004 John C. Paolillo, Instructor.
COE 202: Digital Logic Design Signed Numbers
COMP3221 lec05-numbers-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 5: Number Systems – I
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
King Fahd University of Petroleum and Minerals
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Third Edition.
Data Representation Computer Organization &
1 Integral Data Types in C. 2 First lecture response sent out Lots of good questions Some questions will be punted to reading when appropriate 3 statements.
Data Representation COE 205
Assembly Language and Computer Architecture Using C++ and Java
Introduction to Programming with Java, for Beginners
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Assembly Language and Computer Architecture Using C++ and Java
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Data Representation. CMPE12cGabriel Hugh Elkaim 2 Data Representation Goal: Store numbers, characters, sets, database records in the computer. What we.
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
1 Integral Data Types in C Professor Jennifer Rexford
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
Computer Science 101 Number Systems. Humans Decimal Numbers (base 10) Decimal Numbers (base 10) Sign-Magnitude (-324) Sign-Magnitude (-324) Decimal Fractions.
Agenda Data Representation Purpose Numbering Systems Binary, Octal, Hexadecimal (Hex) Numbering System Conversions Binary to Octal, Octal to Binary Binary.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Real Numbers and the Decimal Number System
CENG 311 Machine Representation/Numbers
Simple Data Type Representation and conversion of numbers
1 The Design of C: A Rational Reconstruction Jennifer Rexford.
Computers Organization & Assembly Language
Computer Arithmetic Nizamettin AYDIN
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
Binary Numbers.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
Basic Arithmetic (adding and subtracting)
IT253: Computer Organization
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
46 Number Systems Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs Display: Seven segment displays Inputs: Switches.
Lecture 4 Last Lecture –Positional Numbering Systems –Converting Between Bases Today’s Topics –Signed Integer Representation Signed magnitude One’s complement.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
Number Systems and Number Representation Jennifer Rexford 1.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
ECE2030 Introduction to Computer Engineering Lecture 2: Number System Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Tech.
Operations on Bits Arithmetic Operations Logic Operations
Data Representation Bits, Bytes, Binary, Hexadecimal.
69 Decimal (Base 10) Numbers n Positional system - each digit position has a value n 2534 = 2*1, * *10 + 4*1 n Alternate view: Digit position.
Outline Binary Addition 2’s complement Binary Subtraction Half Adder
Data Representation. Goal: Store numbers, characters, sets, database records in the computer. What we got: Circuit that stores 2 voltages, one for logic.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
MicroProcessors Lec. 4 Dr. Tamer Samy Gaafar. Course Web Page —
Data Representation. Representation of data in a computer Two conditions: 1. Presence of a voltage – “1” 2. Absence of a voltage – “0”
Lecture 2: Logic Bryan Burlingame 31 Aug 2016.
David Kauchak CS 52 – Spring 2017
Bits, Data Types, and Operations
Lec 3: Data Representation
Lesson Objectives Aims
Integer Real Numbers Character Boolean Memory Address CPU Data Types
University of Gujrat Department of Computer Science
Binary – Octal - Hexadecimal
Homework Homework Continue Reading K&R Chapter 2 Questions?
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Professor Jennifer Rexford COS 217 Binary Numbers Professor Jennifer Rexford COS 217

Goals of Today’s Lecture Binary numbers Why binary? Converting base 10 to base 2 Octal and hexadecimal Integers Unsigned integers Integer addition Signed integers C bit operators And, or, not, and xor Shift-left and shift-right Function for counting the number of 1 bits Function for XOR encryption of a message

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 Sound Can manipulate in many ways Read and write Logical operations Arithmetic …

Base 10 and Base 2 Divide repeatedly by 2 and keep remainders Each digit represents a power of 10 4173 = 4 x 103 + 1 x 102 + 7 x 101 + 3 x 100 Base 2 Each bit represents a power of 2 10110 = 1 x 24 + 0 x 23 + 1 x 22 + 0 x 20 = 22 Divide repeatedly by 2 and keep remainders 12/2 = 6 R = 0 6/2 = 3 R = 0 3/2 = 1 R = 1 1/2 = 0 R = 1 Result = 1100

Writing Bits is Tedious for People Octal (base 8) Digits 0, 1, …, 7 In C: 00, 01, …, 07 Hexadecimal (base 16) Digits 0, 1, …, 9, A, B, C, D, E, F In C: 0x0, 0x1, …, 0xf 0000 = 0 1000 = 8 0001 = 1 1001 = 9 0010 = 2 1010 = A 0011 = 3 1011 = B 0100 = 4 1100 = C 0101 = 5 1101 = D 0110 = 6 1110 = E 0111 = 7 1111 = F Thus the 16-bit binary number 1011 0010 1010 1001 converted to hex is B2A9

Representing Colors: RGB Three primary colors Red Green Blue Strength 8-bit number for each color (e.g., two hex digits) So, 24 bits to specify a color In HTML, on the course Web page Red: <font color="#FF0000"><i>Symbol Table Assignment Due</i> Blue: <font color="#0000FF"><i>Fall Recess</i></font> Same thing in digital cameras Each pixel is a mixture of red, green, and blue

Storing Integers on the Computer Fixed number of bits in memory Short: usually 16 bits Int: 16 or 32 bits Long: 32 bits Unsigned integer No sign bit Always positive or 0 All arithmetic is modulo 2n Example of unsigned int 00000001  1 00001111  15 00010000  16 00100001  33 11111111  255

Adding Two Integers: Base 10 From right to left, we add each pair of digits We write the sum, and add the carry to the next column 0 1 1 + 0 0 1 Sum Carry 1 9 8 + 2 6 4 Sum Carry 4 6 1 2 1 1 1 1

Binary Sums and Carries a b Sum a b Carry 0 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 1 1 AND XOR 69 0100 0101 103 + 0110 0111 172 1010 1100

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 2n doesn’t change the answer For eight-bit number, n=8 and 2n=256 E.g., (37 + 256) mod 256 is simply 37 This can help us do subtraction… Suppose you want to compute a – b Note that this equals a + (256 -1 - b) + 1

One’s and Two’s Complement One’s complement: flip every bit E.g., b is 01000101 (i.e., 69 in base 10) One’s complement is 10111010 That’s simply 255-69 Subtracting from 11111111 is easy (no carry needed!) Two’s complement Add 1 to the one’s complement E.g., (255 – 69) + 1  1011 1011 1111 1111 - 0100 0101 b one’s complement 1011 1010

Putting it All Together Computing “a – b” for unsigned integers Same as “a + 256 – b” Same as “a + (255 – b) + 1” Same as “a + onecomplement(b) + 1” Same as “a + twocomplement(b)” Example: 172 – 69 The original number 69: 0100 0101 One’s complement of 69: 1011 1010 Two’s complement of 69: 1011 1011 Add to the number 172: 1010 1100 The sum comes to: 0110 0111 Equals: 103 in base 10 1010 1100 + 1011 1011 1 0110 0111

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

Overflow: Running Out of Room Adding two large integers together Sum might be too large to store in the number of bits allowed What happens? Unsigned numbers 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 In this case, fixable by using “long”…

Bitwise Operators: AND and OR Bitwise AND (&) Mod on the cheap! E.g., h = 53 & 15; Bitwise OR (|) | 1 & 1 53 1 1 1 1 & 15 1 1 1 1 5 1 1

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 & ~7; XOR (^) 0 if both bits are the same 1 if the two bits are different ^ 1 1 1 1

Bitwise Operators: Shift Left/Right Shift left (<<): Multiply by powers of 2 Shift some # of bits to the left, filling the blanks with 0 Shift right (>>): Divide by powers of 2 Shift some # of bits to the right For unsigned integer, fill in blanks with 0 What about signed integers? Varies across machines… Can vary from one machine to another! 53 1 1 1 53<<2 1 1 1 53 1 1 1 53>>2 1 1 1

Count Number of 1s in an Integer Function bitcount(unsigned x) Input: unsigned integer Output: number of bits set to 1 in the binary representation of x Main idea Isolate the last bit and see if it is equal to 1 Shift to the right by one bit, and repeat int bitcount(unsigned x) { int b; for (b=0; x!=0; x >>= 1) if (x & 01) b++; return b; }

XOR Encryption Program to encrypt text with a key Input: original text in stdin Output: encrypted text in stdout Use the same program to decrypt text with a key Input: encrypted text in stdin Output: original text in stdout Basic idea Start with a key, some 8-bit number (e.g., 0110 0111) Do an operation that can be inverted E.g., XOR each character with the 8-bit number 0100 0101 0010 0010 ^ 0110 0111 ^ 0110 0111 0010 0010 0100 0101

XOR Encryption, Continued But, we have a problem Some characters are control characters These characters don’t print So, let’s play it safe If the encrypted character would be a control character … just print the original, unencrypted character Note: the same thing will happen when decrypting, so we’re okay C function iscntrl() Returns true if the character is a control character

XOR Encryption, C Code #define KEY ‘&’ int main() { int orig_char, new_char; while ((orig_char = getchar()) != EOF) { new_char = orig_char ^ KEY; if (iscntrl(new_char)) putchar(orig_char); else putchar(new_char); } return 0;

Conclusions 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, and XOR Shift left and shift right Useful for efficient and concise code, though sometimes cryptic

Next Week Canceling second precept Monday lecture time Monday/Tuesday precept as usual Canceling the Wednesday/Thursday precept due to midterms Monday lecture time Review for the midterm exam Come with questions in tow! Wednesday lecture time Midterm exam Open book and open notes Practice exams online