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

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

Arithmetic and Bitwise Operations on Binary Data

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?

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

Bits, Bytes and Integers Part 1

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

CSCI2467: Systems Programming Concepts

Bits, Bytes, and Integers

Bits, Bytes, and Integers

Bits and Bytes January 13, 2005

Data III & Integers I

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Lecture 5-6: Bits, Bytes, and Integers

Data III & Integers I

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

Hardware: Logical View

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

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

Bits, Bytes and Integers

Manipulating Integers

Systems Programming and Computer Architecture ( )

Bitwise Data Manipulation. Bitwise operations More on integers

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

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

Integer Encoding and Manipulation

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

Data III & Integers I

Bits, Bytes, and Integers August 26, 2009

Representing and Manipulating Integers. Jo, Heeseung

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

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

Computer Systems CEN591(502) Fall 2011

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

JAVA OPERATORS GENERAL

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Representing and Manipulating Integers Part I

CS 107 Lecture 2: Bits and Bytes (continued)

Bits, Bytes, and Integers Part 2

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

EE292: Fundamentals of ECE

Integer Representation

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

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

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Digital Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Computer Organization: A Programmer's Perspective

Computer Organization & Systems Exam I Example Questions

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CS 253. January 14, 2017

Chapter 4. Operations on Data

Chapter 2. Instruction Set. RISC vs. CISC Instruction set. The University of Adelaide, School of Computer Science 18 September 2017

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

REMEMBER TO REGISTER FOR THE EXAM.

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Logical and Bitwise Expressions

A flow chart is a graphical or symbolic representation of a process.

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Foundations of Computer Systems

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

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

Number System. Introduction. Decimal Numbers

COMP2611: Computer Organization. Data Representation

Beginning C Programming for Engineers

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

CS 261 Fall Mike Lam, Professor Integer Encodings

CS & IT Conversions. Magnitude 10,000 1,

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

Arithmetic Operations

Where we are going (today)

Operators. Java operators are classified into three categories:

AGENDA Binary Operations CS 3330 Samira Khan

CS 24: INTRODUCTION TO. Spring 2015 Lecture 2 COMPUTING SYSTEMS

CS33 Project Gear Up. Data

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

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

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

Bits, Words, and Integers

Binary Values. CSE 410 Lecture 02

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

More about Binary 9/6/2016

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

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

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

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

Chapter 3: Operators, Expressions and Type Conversion

Chapter 3: Arithmetic for Computers

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

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Arithmetic Operators. Portability: Printing Numbers

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Transcription:

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

Number Representation Hindu-Arabic numerals developed by Hindus starting in 5 th century» positional notation» symbol for 0 adopted and modified somewhat later by Arabs» known by them as Rakam Al-Hind (Hindu numeral system) 1999 rather than MCMXCIX» (try doing long division with Roman numerals!) CS33 Intro to Computer Systems VII 2 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Which Base? 1999 base 10» 9 10 0 +9 10 1 +9 10 2 +1 10 3 base 2» 11111001111 1 2 0 +1 2 1 +1 2 2 +1 2 3 +0 2 4 +0 2 5 +1 2 6 +1 2 7 +1 2 8 +1 2 9 +1 2 10 base 8» 3717 7 8 0 +1 8 1 +7 8 2 +3 8 3» why are we interested? base 16» 7CF 15 16 0 +12 16 1 +7 16 2» why are we interested? CS33 Intro to Computer Systems VII 3 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Words 0 1 1 1 1 1 0 0 1 1 1 1 12-bit computer word 3 7 1 7 0 0 0 0 0 1 1 1 1 1 0 0 1 1 1 1 16-bit computer word 0 7 C F CS33 Intro to Computer Systems VII 4 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Algorithm void basex(unsigned int num, unsigned int base) { char digits[] = {'0', '1', '2', '3', '4', '5', '6', }; char buf[8*sizeof(unsigned int)+1]; int i; for (i = sizeof(buf) - 2; i >= 0; i--) { buf[i] = digits[num%base]; } num /= base; if (num == 0) break; } buf[sizeof(buf) - 1] = '\0'; printf("%s\n", &buf[i]); CS33 Intro to Computer Systems VII 5 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Or $ bc obase=16 1999 7CF $ CS33 Intro to Computer Systems VII 6 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 1 What s the decimal (base 10) equivalent of 23 16? a) 19 b) 33 c) 35 d) 37 CS33 Intro to Computer Systems VII 7 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Encoding Byte Values Byte = 8 bits binary 000000002 to 111111112 decimal: 010 to 25510 hexadecimal 0016 to FF16» base 16 number representation» use characters 0 to 9 and A to F» write FA1D37B16 in C as 0xFA1D37B 0xfa1d37b 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 CS33 Intro to Computer Systems VII 8 Copyright 2017 Thomas W. Doeppner. All rights reserved.

And Boolean Algebra Developed by George Boole in 19th Century algebraic representation of logic» encode true as 1 and false as 0 n A&B = 1 when both A=1 and B=1 Or n A B = 1 when either A=1 or B=1 Not n ~A = 1 when A=0 Exclusive-Or (Xor) n A^B = 1 when either A=1 or B=1, but not both CS33 Intro to Computer Systems VII 9 Copyright 2017 Thomas W. Doeppner. All rights reserved.

General Boolean Algebras Operate on bit vectors operations applied bitwise 01101001 01010101 01101001 & 01010101 01101001 ^ 01010101 ~ 01010101 01000001 01111101 00111100 10101010 All of the properties of boolean algebra apply CS33 Intro to Computer Systems VII 10 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Example: Representing & Manipulating Sets Representation width-w bit vector represents subsets of {0,, w 1} a j = 1 iff j A 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 } CS33 Intro to Computer Systems VII 11 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Bit-Level Operations in C Operations &,, ~, ^ available in C apply to any integral data type» long, int, short, char view arguments as bit vectors arguments applied bit-wise Examples (char datatype) ~0x41 0xBE ~010000012 101111102 ~0x00 0xFF ~000000002 111111112 0x69 & 0x55 0x41 011010012 & 010101012 010000012 0x69 0x55 0x7D 011010012 010101012 011111012 CS33 Intro to Computer Systems VII 12 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Contrast: Logic Operations in C Contrast to Logical Operators &&,,!» view 0 as false» anything nonzero as true» always return 0 or 1» early termination/short-circuited execution Examples (char datatype)!0x41 0x00!0x00 0x01!!0x41 0x01 0x69 && 0x55 0x01 0x69 0x55 0x01 p && *p (avoids null pointer access) CS33 Intro to Computer Systems VII 13 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Contrast: Logic Operations in C Contrast to Logical Operators &&,,!» view 0 as false» anything nonzero as true Watch» always out return for 0 && or 1 vs. & (and vs. )» early termination/short-circuited execution One of the more common oopsies in Examples C programming (char datatype)!0x41 0x00!0x00 0x01!!0x41 0x01 0x69 && 0x55 0x01 0x69 0x55 0x01 p && *p (avoids null pointer access) CS33 Intro to Computer Systems VII 14 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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 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 Argument x << 3 Log. >> 2 Arith. >> 2 Argument x << 3 Log. >> 2 Arith. >> 2 01100010 00010000 00011000 00011000 10100010 00010000 00101000 11101000 CS33 Intro to Computer Systems VII 15 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Unsigned Integers b w-1 b w-2 b w-3 b 2 b 1 b 0 value = w'1! b i $ 2 i i)0 CS33 Intro to Computer Systems VII 16 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Signed Integers Sign-magnitude b w-1 b w-2 b w-3 b 2 b 1 b 0 sign magnitude value = two representations of zero! computer must have two sets of instructions one for signed arithmetic, one for unsigned CS33 Intro to Computer Systems VII 17 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Signed Integers Ones complement negate a number by forming its bitwise complement» e.g., (-1) 01101011 = 10010100 value = = if b w-1 = 0 two zeroes! = if b w-1 = 1 CS33 Intro to Computer Systems VII 18 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Signed Integers Two s complement b w-1 = 0 Þ non-negative number value = b w-1 = 1 Þ negative number one zero! value = CS33 Intro to Computer Systems VII 19 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Signed Integers Negating two s complement value = b w 1 2 w 1 + how to compute value? (~value)+1 w 2 i=0 b i 2 i CS33 Intro to Computer Systems VII 20 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Signed Integers Negating two s complement (continued) value + (~value + 1) = (value + ~value) + 1 = (2 w 1) + 1 = 2 w 0 0 0 0 0 0 w = 1 CS33 Intro to Computer Systems VII 21 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 2 We have a computer with 4-bit words that uses two s complement to represent negative numbers. What is the result of subtracting 0010 (2) from 0001 (1)? a) 0111 b) 1001 c) 1110 d) 1111 CS33 Intro to Computer Systems VII 22 Copyright 2017 Thomas W. Doeppner. All rights reserved.