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

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

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

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Data Representation and Binary Arithmetic. Lecture 2

Chapter 2 Bits, Data Types, and Operations

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Fundamentals of Programming (C)

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

Chapter 3. Information Representation

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Number System (Different Ways To Say How Many) Fall 2016

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Fundamentals of Programming

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

Number Representations

Chapter 2 Number System

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Oberon Data Types. Matteo Corti. December 5, 2001

3.1. Unit 3. Binary Representation

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

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

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Fundamental Data Types

EE 109 Unit 2. Binary Representation Systems

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

ASSIGNMENT 5 TIPS AND TRICKS

Do not start the test until instructed to do so!

Do not start the test until instructed to do so!

Number Systems Base r

Do not start the test until instructed to do so!

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Chapter 8. Characters and Strings

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

Characters Lesson Outline

Simple Data Types in C. Alan L. Cox

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

Positional Number System

The Binary Number System

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor.

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

Source coding and compression

Lecture (09) x86 programming 8

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

CSE 30 Spring 2007 Final Exam

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

n NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

CPSC 301: Computing in the Life Sciences Lecture Notes 16: Data Representation

CSE 30 Fall 2007 Final Exam

FD-011WU. 2D Barcode Reader User Guide V1.6CC

Variables and data types

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2

Midterm Exam, Fall 2015 Date: October 29th, 2015

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

CSE 30 Fall 2012 Final Exam

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

CSE 30 Fall 2013 Final Exam

Bits, Bytes, and Integers Part 2

The following are the data types used in the C programming language:

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Table of Contents Sleep Settings How to Configure the Scanner. 7 Chapter 2 System Setup

CSE 30 Winter 2009 Final Exam

Midterm CSE 131 Winter 2012

Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 30 Winter 2014 Final Exam

CSE 30 Spring 2006 Final Exam

ASCII Code - The extended ASCII table

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

6.096 Introduction to C++ January (IAP) 2009

Data Types Literals, Variables & Constants

S-Series Sensor ASCII Protocol v8.1.0

CSC 1107: Structured Programming

Final CSE 131 Winter 2010

Appendix A Developing a C Program on the UNIX system

CSE 30 Fall 2006 Final Exam

Chapter 1. Hardware. Introduction to Computers and Programming. Chapter 1.2

Chemistry Hour Exam 2

Connecting UniOP to Datalogic Barcode Readers

Serial I/O. 4: Serial I/O. CET360 Microprocessor Engineering. J. Sumey

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Transcription:

CSC 2400: Computer Systems Data Representa5on What kinds of data do we need to represent? - Numbers signed, unsigned, integers, floating point, complex, rational, irrational, - Text characters, strings, - Images pixels, colors, shapes, - Sound - Logical true, false - Instructions - q Data type: - representation and operations within the computer 1

Word-Oriented Memory Organization q esses Specify Byte Locations - ess of first byte in word - esses of successive words differ by 4 (32-bit) or 8 (64-bit) 32-bit 64-bit Words Words Bytes. 0000?? 0004?? 0008?? 0012?? 0000?? 0008?? 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 Data Representations q Sizes of C Objects (in Bytes) C Data Type Sparc/Unix Typical 32-bit Intel IA32 int 4 4 4 long int 8 4 4 char 1 1 1 short 2 2 2 float 4 4 4 double 8 8 8 long double 8 8 10/12 char * 8 4 4 (or any other pointer) 2

Byte Ordering q How should bytes within multi-byte word be ordered in memory? q Conventions - Sun s, Mac s are Big Endian machines o Least significant byte has highest address - Alphas, PC s are Little Endian machines o Least significant byte has lowest address Byte Ordering Example q Big Endian - Least significant byte has highest address q Little Endian - Least significant byte has lowest address q Example - Variable x has 4-byte representation 0x01234567 - ess given by &x is 0x100 Big Endian Little Endian 0x100 0x101 0x102 0x103 01 23 45 67 0x100 0x101 0x102 0x103 67 45 23 01 3

Integers Unsigned Integers q An n-bit unsigned integer represents 2 n values: from 0 to 2 n -1. 2 2 2 1 2 0 0 0 0 0 0 0 1 1 0 1 0 2 0 1 1 3 1 0 0 4 1 0 1 5 1 1 0 6 1 1 1 7 4

Signed Integers q How do computers differen1ate between posi1ve and nega1ve integers? - Posi1ve integers have most significant bit 0 - Nega1ve integers have most significant bit 1 q Nega1ve integer representa1ons: 1. Sign Magnitude 2. One s Complement 3. Two s Complement 1. Sign Magnitude q Use the leimost bit to store the sign - Zero for posi1ve number - One for nega1ve number q Examples 0 0 1 0 1 1 0 0 è 44 1 0 1 0 1 1 0 0 è -44 Sign Magnitude q Hard to do arithme1c this way, so it is rarely used - What is the result of 44 44? 5

1. Sign Magnitude (contd.) 0 0 1 0 1 1 0 0 è 44 1 0 1 0 1 1 0 0 è -44 Sign Magnitude q For numbers represented on n bits: - Range of posi1ve integers: from 0 to (2 n-1 1) - Range of nega1ve integers: from (2 n-1 1) to 1 2. One s Complement q LeImost bit is 0 for posi1ve numbers 0 0 1 0 1 1 0 0 è 44 q To obtain the corresponding nega1ve number (- 44), flip every bit: 1 1 0 1 0 0 1 1 è -44 q So - 44 is the one s complement of 44. 6

2. One s Complement (contd.) q What is the result of 44 44? 0 0 1 0 1 1 0 0 ( 44) 1 1 0 1 0 0 1 1 (-44) q Issue: two different representa1ons for zero 3. Two s Complement q LeImost bit is 0 for posi1ve numbers 0 0 1 0 1 1 0 0 è 44 q To obtain the corresponding nega1ve number - 44, add 1 to the one s complement of 44: 1 1 0 1 0 0 1 1 è one s complement + 0 0 0 0 0 0 0 1 1 1 0 1 0 1 0 0 è two s complement 7

3. Two s Complement (contd.) q What is the result of 44 44? 0 0 1 0 1 1 0 0 ( 44) 1 1 0 1 0 1 0 0 (-44) q Used by most computer systems q For numbers represented on n bits: - Range of posi1ve integers: - Range of nega1ve integers: from 0 to (2 n-1 1) from 2 n-1 to 1 Converting Two s Complement to Decimal 1. If leading bit is one, take two s complement to get a positive number. 2. Add powers of 2 that have 1 in the corresponding bit positions. 3. If original number was negative, add a minus sign. X 01101000 two 2 6 +2 5 +2 3 64+32+8 104 ten Assuming 8-bit 2 s complement numbers. n 2 n 0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024 8

More Examples X 00100111 two 2 5 +2 2 +2 1 +2 0 32+4+2+1 39 ten X 11100110 two -X 00011010 2 4 +2 3 +2 1 16+8+2 26 ten X -26 ten n 2 n 0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024 Assuming 8-bit 2 s complement numbers. Two s Complement Signed Integers q Most significant bit is sign bit and has weight 2 n-1. q Range of an n-bit number: -2 n-1 through 2 n-1 1. - The most negative number (-2 n-1 ) has no positive counterpart. -2 3 2 2 2 1 2 0 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-2 3 2 2 2 1 2 0 1 0 0 0-8 1 0 0 1-7 1 0 1 0-6 1 0 1 1-5 1 1 0 0-4 1 1 0 1-3 1 1 1 0-2 1 1 1 1-1 9

Number Representa5ons Review q LeImost bit zero indicates q Sign Magnitude: - nega1ve values q One s Complement: - nega1ve values q Two s complement: - nega1ve values positive number most significant bit 1 has weight 0 are the 1 s complement of positive values (flip every bit) are the 2 s complement of positive values most significant bit 1 has weight 2 n-1 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 10

Ques5on q What value does 10011001 represent? ASCII 11

The ASCII Code American Standard Code for Information Interchange 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI 16 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 32 SP! " # $ % & ' ( ) * +, -. / 48 0 1 2 3 4 5 6 7 8 9 : ; < >? 64 @ A B C D E F G H I J K L M N O 80 P Q R S T U V W X Y Z [ \ ] ^ _ 96 ` a b c d e f g h i j k l m n o 112 p q r s t u v w x y z { } ~ DEL Lower case: 97-122 and upper case: 65-90 E.g., a is 97 and A is 65 (i.e., 32 apart) char Constants q C has char constants (sort of) q Examples Constant Binary Representation (assuming ASCII) Note 'a' 01100001 letter '0' 00110000 digit '\x61' 01100001 hexadecimal form Use single quotes for char constant Use double quotes for string constant * Technically 'a' is of type int; automatically truncated to type char when appropriate 12

More char Constants Escape characters Constant Binary Representation (assuming ASCII) Note '\a' 00000111 alert (bell) '\b' 00001000 backspace '\f' 00001100 form feed '\n' 00001010 newline '\r' 00001101 carriage return '\t' 00001001 horizontal tab '\v' 00001011 vertical tab '\\' 01011100 backslash '\?' 00111111 question mark '\'' 00100111 single quote '\"' 00100010 double quote '\0' 00000000 null Used often Interesting Properties of ASCII Code q What is relationship between a decimal digit ('0', '1', ) and its ASCII code? q What is the difference between an upper-case letter ('A', 'B', ) and its lower-case equivalent ('a', 'b', )? q Given two ASCII characters, how do we tell which comes first in alphabetical order? q Are 128 characters enough? (http://www.unicode.org/) 13

Other Data Types q Floating Points - IEEE representation, to be covered later q Text strings - sequence of characters, terminated with NULL (0) q Image - array of pixels o monochrome: one bit (1/0 black/white) o color: red, green, blue (RGB) components (e.g., 8 bits each) o other properties: transparency q Sound - sequence of fixed-point numbers Limits of the Machine: How much memory space for data? 14

Storage Units 1 bit smallest unit of memory 1 byte 8 bits 4 bytes 1 word (system dependent) Words q On most machines, bytes are assembled into larger structures called words, where a word is usually defined to be the number of bits the processor can operate on at one 1me. q Some machines use four- byte words (32 bits), while some others use 8- byte words (64 bits) and some machines use less conven1onal sizes. 32-bit 64-bit Words Words Bytes. 0000?? 0004?? 0008?? 0012?? 0000?? 0008?? 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 15

The sizeof Operator Category sizeof Operators sizeof(type) sizeof(expr) q Unique among operators: evaluated at compile- 1me q Evaluates to type size_t; on tanner, same as unsigned int q Examples int i 10; double d 100.0; sizeof(int) /* On tanner, evaluates to 4 */ sizeof(i) /* On tanner, evaluates to 4 */ sizeof(double) /* On tanner, evaluates to 8 */ sizeof(d) /* On tanner, evaluates to 8 */ sizeof(d + 200.0) /* On tanner, evaluates to 8 */ Determining Data Sizes q To determine data sizes on your computer #include <stdio.h> int main() { printf("char: %d\n", (int)sizeof(char)); printf("short: %d\n", (int)sizeof(short)); printf("int: %d\n", (int)sizeof(int)); printf("long: %d\n", (int)sizeof(long)); printf("float: %d\n", ); printf("double: %d\n", ); printf("long double: %d\n", ); return 0; } q Output on tanner char: 1 short: 2 int: 4 long: 4 float: 4 double: 8 long double: 16 16

Overflow: Running Out of Room q Adding two large integers together - Sum might be too large to store in available bits - What happens? 01000 (8) 11000 (-8) + 01001 (9) + 10111 (-9) 10001 (-15) 01111 (+15) Assuming 5-bit 2 s complement numbers. q We have overflow if: - signs of both operands are the same, and - sign of sum is different. Overflow q Unsigned integers - All arithme1c is modulo arithme1c - Sum would just wrap around q Signed integers - Can get nonsense values - Example with 16- bit integers o Sum: 10000+20000+30000 o Result: - 5536 17

Try It Out q Write a program that computes the sum 10000+20000+30000 Use only short int variables in your code: short int a 10000; short int b 20000; short int c 20000; short int sum a + b + c; printf("sum %d\n", sum); Exercise q Assume only four bits are available for represen1ng integers, and signed integers are represented in 2 s complement. q Compute the value of the expression 7 + 7 18

Casting Signed to Unsigned q C Allows Conversions from Signed to Unsigned short int x 15213; unsigned short int ux (unsigned short) x; short int y -15213; unsigned short int uy (unsigned short) y; q Resulting Value - No change in bit representation - Nonnegative values unchanged o ux 15213 - Negative values change into (large) positive values o uy 50323 Try It Out q C code: char a 0xFF; unsigned char b 0xFF; printf("a %d\n", a); printf("b %d\n", b); 19

Int to Char? Try It Out #include <stdio.h> int main() { char c 0x81; int i; } i c; printf(" integer %x\n character %x\n", i, c); i 0x87654321; c i; printf(" integer %d\n character code %d\n", i, c); return 0; C vs. Java: Cast Conversions q Java: demo1ons are not automa1c C: demo1ons are automa1c int i; char c; i c; /* Implicit promotion */ /* Sign extension in Java and C */ c i; /* Implicit demotion */ /* Java: Compiletime error */ /* C: OK; truncation */ c (char)i; /* Explicit demotion */ /* Truncation in Java and C */ 20

What did we learn? q Computer represents everything in binary - Integers, floa1ng- point numbers, characters, - Pixels, sounds, colors, etc. q Memory is bytes, words, endian, two s complement, ASCII, conver1ng between hex- binary- decimal, limits of machine, overflow, cast conversions 21