BITS, BYTES, AND INTEGERS

Similar documents
Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Arithmetic and Bitwise Operations on Binary Data

Bits, Bytes and Integers Part 1

Arithmetic and Bitwise Operations on Binary Data

Bits, Bytes, and Integers

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Bits, Bytes, and Integers

Lecture 5-6: Bits, Bytes, and Integers

CSCI2467: Systems Programming Concepts

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Computer Systems CEN591(502) Fall 2011

CS140 Lecture 08: Data Representation: Bits and Ints. John Magee 13 February 2017

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

Integer Encoding and Manipulation

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Systems Programming and Computer Architecture ( )

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Manipulating Integers

Representing and Manipulating Integers. Jo, Heeseung

Bits, Bytes, and Integers August 26, 2009

Integers. Dr. Steve Goddard Giving credit where credit is due

Integers Sep 3, 2002

Bits and Bytes January 13, 2005

Integers. Today. Next time. ! Numeric Encodings! Programming Implications! Basic operations. ! Floats

Bits, Bytes and Integers

Systems 1. Integers. Unsigned & Twoʼs complement. Addition, negation, multiplication

Foundations of Computer Systems

Topics of this Slideset. CS429: Computer Organization and Architecture. Encoding Integers: Unsigned. C Puzzles. Integers

CS429: Computer Organization and Architecture

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

Computer Organization: A Programmer's Perspective

Bits, Bytes, and Integers Part 2

Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

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

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

Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing

Data III & Integers I

Data III & Integers I

Representing and Manipulating Integers Part I

Integer Arithmetic. CS 347 Lecture 03. January 20, 1998

Hardware: Logical View

Bits, Bytes, and Integers. Bits, Bytes, and Integers. The Decimal System and Bases. Everything is bits. Converting from Decimal to Binary

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

Topics of this Slideset. CS429: Computer Organization and Architecture. It s Bits All the Way Down. Why Binary? Why Not Decimal?

CS429: Computer Organization and Architecture

Page 1 CISC360. Encoding Integers Unsigned. Integers Sep 8, Numeric Ranges. Encoding Example (Cont.)

Bitwise Data Manipulation. Bitwise operations More on integers

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Page # CISC360. Integers Sep 11, Encoding Integers Unsigned. Encoding Example (Cont.) Topics. Twoʼs Complement. Sign Bit

Integers II. CSE 351 Autumn 2018

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CSE 351 Midterm - Winter 2015 Solutions

Data III & Integers I

Computer Organization & Systems Exam I Example Questions

C Puzzles! Taken from old exams. Integers Sep 3, Encoding Integers Unsigned. Encoding Example (Cont.) The course that gives CMU its Zip!

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

CSE 351 Midterm - Winter 2015

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

CS:APP Web Aside DATA:TNEG: Bit-Level Representation of Two s Complement Negation

CSE 351 Midterm - Winter 2017

EE292: Fundamentals of ECE

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS 270: Computer Organization. Integer Arithmetic Operations

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

Simple Data Types in C. Alan L. Cox

Representing Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS367 Test 1 Review Guide

Logical and Bitwise Expressions

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

CS 107 Lecture 2: Bits and Bytes (continued)

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

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

CS 253. January 14, 2017

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

JAVA OPERATORS GENERAL

Integer Representation Floating point Representation Other data types

Integer Representation

Representing Integers

REMEMBER TO REGISTER FOR THE EXAM.

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

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

Bits, Words, and Integers

CS 261 Fall Mike Lam, Professor Integer Encodings

COMP2611: Computer Organization. Data Representation

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

SECTION II: LANGUAGE BASICS

Beginning C Programming for Engineers

Digital Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

COMS 1003 Fall Introduction to Computer Programming in C. Bits, Boolean Logic & Discrete Math. September 13 th

Programming Lecture 3

Binary Values. CSE 410 Lecture 02

COMP 122/L Lecture 2. Kyle Dewey

LAB A Translating Data to Binary

Chapter 4. Operations on Data

Transcription:

BITS, BYTES, AND INTEGERS CS 045 Computer Organization and Architecture Prof. Donald J. Patterson Adapted from Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

ORIGINS Credited to George Boole (1815-1864) Laws of Thought (1854)

ORIGINS Credited to George Boole (1815-1864) Laws of Thought (1854)

ORIGINS Credited to George Boole (1815-1864) Laws of Thought (1854) No general method for the solution of questions in the theory of probabilities can be established which does not explicitly recognise, not only the special numerical bases of the science, but also those universal laws of thought which are the basis of all reasoning, and which, whatever they may be as to their essence, are at least mathematical as to their form.

ORIGINS Credited to George Boole (1815-1864) Laws of Thought (1854)

BOOLEAN ALGEBRA ORIGINS Credited to George Boole (1815-1864) Laws of Thought (1854)

ORIGINS Credited to George Boole (1815-1864) Laws of Thought (1854) He compared the Trinity to the 3 axes of (x,y,z) Wikipedia entry on Boole

ORIGINS Credited to George Boole (1815-1864) Laws of Thought (1854) He compared the Trinity to the 3 axes of (x,y,z) Wikipedia entry on Boole

AXIOMS represent 1 as true 0 as false

FOUR FOUNDATIONAL OPERATIONS AND (&) A&B = 1 when both A=1 and B=1

FOUR FOUNDATIONAL OPERATIONS OR ( ) A B = 1 when either A=1 or B=1

FOUR FOUNDATIONAL OPERATIONS NOT (~) ~A = 1 when A=0

FOUR FOUNDATIONAL OPERATIONS Exclusive-OR XOR (^) A^B = 1 when either A=1 or B=1, but not both

FOUR FOUNDATIONAL OPERATIONS

THESE CAN BE APPLIED TO BIT-VECTORS Operations can be applied bit-wise All the properties of Boolean Algebra apply 01101001 & 01010101 01000001 01101001 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 01000001 01111101 00111100 10101010

THESE CAN BE APPLIED TO BIT-VECTORS Operations can be applied bit-wise All the properties of Boolean Algebra apply 01101001 & 01010101 01000001 01101001 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 01000001 01111101 00111100 10101010

THESE CAN BE APPLIED TO BIT-VECTORS Operations can be applied bit-wise All the properties of Boolean Algebra apply 01101001 & 01010101 01000001 01101001 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 01000001 01111101 00111100 10101010

THESE CAN BE APPLIED TO BIT-VECTORS Operations can be applied bit-wise All the properties of Boolean Algebra apply 01101001 & 01010101 01000001 01101001 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 01000001 01111101 00111100 10101010

THESE CAN BE APPLIED TO BIT-VECTORS Operations can be applied bit-wise All the properties of Boolean Algebra apply 01101001 & 01010101 01000001 01101001 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 01000001 01111101 00111100 10101010

BIT-VECTORS CAN REPRESENT SETS a w-bit bit-vector can encode the membership of w elements in a set aj = 1 if j A 01101001 { 0, 3, 5, 6 } 76543210 01010101 { 0, 2, 4, 6 } 76543210

BIT-VECTORS CAN REPRESENT SETS 01101001 { 0, 3, 5, 6 } 76543210 01010101 { 0, 2, 4, 6 } 76543210 Operations & Intersection 01000001 { 0, 6 } Union 01111101 { 0, 2, 3, 4, 5, 6 } ^ Symmetric difference 00111100 { 2, 3, 4, 5 } ~ Complement 10101010 { 1, 3, 5, 7 }

BOOLEAN ALGEBRA IN C Operations &,, ~, ^ Available in C Apply to any integral data type long, int, short, char, unsigned View arguments as bit vectors Arguments applied bit-wise

BOOLEAN ALGEBRA IN C Examples (Char data type) ~0x41 0xBE ~010000012 101111102 ~0x00 0xFF ~000000002 111111112 0x69 & 0x55 0x41 011010012 & 010101012 010000012 0x69 0x55 0x7D 011010012 010101012 011111012

CONTRAST TO LOGIC OPERATIONS IN C Contrast to Logical Operators &&,,! View 0 as False Anything nonzero as True Always return 0 or 1 Early termination

CONTRAST TO LOGIC OPERATIONS IN C Examples (char data type)!0x41 0x00!0x00 0x01!!0x41 0x01 0x69 && 0x55 0x01 0x69 0x55 0x01 p && *p (avoids null pointer access)

CONTRAST TO LOGIC OPERATIONS IN C Examples (char data type)!0x41 0x00!0x00 0x01!!0x41 0x01 0x69 && 0x55 0x01 0x69 0x55 0x01 p && *p (avoids null pointer access)

CONTRAST TO LOGIC OPERATIONS IN C Examples (char data type) Don t confuse!0x41 0x00 && for &!0x00 0x01 for!!0x41 0x01! for ~ pow(x,y) for ^ 0x69 && 0x55 0x01 common C error 0x69 0x55 0x01 p && *p (avoids null pointer access)

SHIFT OPERATIONS Left Shift: x << y Shift bit-vector x left y positions Throw away extra bits on left Fill with 0 s on right

SHIFT OPERATIONS Right Shift: x >> y Shift bit-vector x right y positions Throw away extra bits on right Logical shift Fill with 0 s on left Arithmetic shift Replicate most significant bit on left Undefined Behavior Shift amount < 0 or word size

SHIFT OPERATIONS Argument x 01100010 << 3 Log. >> 2 00010000 00011000 x << 3 x >> 2 x >> 2 Arith. >> 2 00011000 Argument x 10100010 << 3 Log. >> 2 Arith. >> 2 00010000 00101000 11101000

ENCODING INTEGERS UNSIGNED w 1 B2U(X) = x i 2 i i=0

ENCODING INTEGERS SIGNED - TWO S COMPLEMENT w 2 i=0 B2T(X) = x w 1 2 w 1 + x i 2 i

ENCODING INTEGERS SIGNED - TWO S COMPLEMENT w 2 i=0 B2T(X) = x w 1 2 w 1 + x i 2 i

ENCODING INTEGERS SIGNED - TWO S COMPLEMENT w 2 i=0 B2T(X) = x w 1 2 w 1 + x i 2 i That s the sign bit

ENCODING INTEGERS SIGNED - TWO S COMPLEMENT w 2 i=0 B2T(X) = x w 1 2 w 1 + x i 2 i That s the sign bit

ENCODING INTEGERS UNSIGNED AND SIGNED INTEGERS C short 2 bytes long Decimal Hex Binary x 15213 3B 6D 00111011 01101101 y -15213 C4 93 11000100 10010011 w 1 i=0 B2U(X) = x i 2 i w 2 i=0 B2T(X) = x w 1 2 w 1 + x i 2 i Sign Bit For 2 s complement, most significant bit indicates sign 0 for nonnegative 1 for negative

ENCODING INTEGERS TWO S COMPLEMENT EXAMPLE x = 15213: 00111011 01101101 y = -15213: 11000100 10010011 Weight 15213-15213 1 1 1 1 1 2 0 0 1 2 4 1 4 0 0 8 1 8 0 0 16 0 0 1 16 32 1 32 0 0 64 1 64 0 0 128 0 0 1 128 256 1 256 0 0 512 1 512 0 0 1024 0 0 1 1024 2048 1 2048 0 0 4096 1 4096 0 0 8192 1 8192 0 0 16384 0 0 1 16384-32768 0 0 1-32768 Sum 15213-15213

ENCODING INTEGERS SIGNED - TWO S COMPLEMENT

ENCODING INTEGERS SIGNED - TWO S COMPLEMENT

ENCODING INTEGERS SIGNED - TWO S COMPLEMENT Why do we use Two s Complement instead of something easier like just a sign bit?

ENCODING INTEGERS NUMERIC RANGES Unsigned Values UMin = 0 000 0 UMax = 2 w 1 111 1 Two s Complement Values TMin = 2 w 1 100 0 TMax = 2 w 1 1 011 1 Other Values Minus 1 111 1

ENCODING INTEGERS VALUES FOR 2 BYTE WORD (W = 16) Decimal Hex Binary UMax 65535 FF FF 11111111 11111111 TMax 32767 7F FF 01111111 11111111 TMin -32768 80 00 10000000 00000000-1 -1 FF FF 11111111 11111111 0 0 00 00 00000000 00000000

ENCODING INTEGERS VALUES FOR DIFFERENT WORD SIZES W 8 16 32 64 UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615 TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807 TMin -128-32,768-2,147,483,648-9,223,372,036,854,775,808 Observations TMin = TMax + 1 Asymmetric range UMax = 2 * TMax + 1

ENCODING INTEGERS UNSIGNED AND SIGNED NUMERIC VALUES X B2U(X) B2T(X) 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 7 1010 10 6 1011 11 5 1100 12 4 1101 13 3 1110 14 2 1111 15 1 Equivalence Same encodings for nonnegative values Uniqueness Every bit pattern represents unique integer value Each representable integer has unique bit encoding Can Invert Mappings U2B(x) = B2U -1 (x) Bit pattern for unsigned integer T2B(x) = B2T -1 (x) Bit pattern for two s comp integer