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

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

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

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

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

Chapter 2 Bits, Data Types, and Operations

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

Fundamentals of Programming (C)

Data Representation and Binary Arithmetic. Lecture 2

Fundamentals of Programming

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

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

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

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

3.1. Unit 3. Binary Representation

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Chapter 2 Number System

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

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

Chapter 3. Information Representation

Number Representations

EE 109 Unit 2. Binary Representation Systems

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.

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

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

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

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

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

ASSIGNMENT 5 TIPS AND TRICKS

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

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

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

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

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

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

Bits, Bytes, and Integers

Arithmetic and Bitwise Operations on Binary Data

Number Systems Base r

Oberon Data Types. Matteo Corti. December 5, 2001

Bits and Bytes January 13, 2005

Do not start the test until instructed to do so!

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

The Design of C: A Rational Reconstruction"

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

Bits, Bytes, and Integers Part 2

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

Lecture 5-6: Bits, Bytes, and Integers

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

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

Bits, Bytes, and Integers

Do not start the test until instructed to do so!

Topic Notes: Bits and Bytes and Numbers

Bits, Bytes and Integers

The Binary Number System

Positional Number System

Simple Data Types in C. Alan L. Cox

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

Hardware: Logical View

The Design of C: A Rational Reconstruction

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

Fundamental Data Types

Characters Lesson Outline

Topic Notes: Bits and Bytes and Numbers

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

Do not start the test until instructed to do so!

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.

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

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

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

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

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

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

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

Chapter 8. Characters and Strings

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

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

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

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

Computer Systems CEN591(502) Fall 2011

CSE 30 Fall 2013 Final Exam

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

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

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

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

COMP2121: Microprocessors and Interfacing. Number Systems

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

CSE 30 Winter 2014 Final Exam

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

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

The Design of C: A Rational Reconstruction

Lecture 2: Number Representa2on

Binary Representations and Arithmetic

Variables and data types

CSE 30 Fall 2007 Final Exam

Representing and Manipulating Integers. Jo, Heeseung

Data Representation 1

Transcription:

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

Analog vs. Digital System q Analog Signals - Value varies con1nuously q Digital Signals - Value limited to a finite set - Digital systems more robust q Binary Signals - Has at most 2 values - Used to represent bit values - Bit 1me T needed to send 1 bit Why Bits (Binary Digits)? q 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 q Can represent many kinds of informa1on - Boolean (true or false) - Numbers (23, 79, ) - Characters ( a, z, ) - Pixels, sounds - Internet addresses 2

Coding q A single binary input can have two values: 1 or 0 q More bits = more combina1ons 0 0 0 1 1 0 1 1 Coding q How many values can you represent on 3 bits? q What about n bits? 3

Binary Numbers q How do you figure out what the value of 1110 is? - Same way you do for 4173, for instance q 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 q Binary (base 2) - Each bit represents a power of 2-1110 2 = 1 x 2 3 + 1 x 2 2 + 1 x 2 1 + 0 x 2 0 = 14 10 Coun3ng in Binary 0 = 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 = 12 = 13 = 14 = 15 = 16 = 17 = 18 = 00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 01010 01011 01100 01101 01110 01111 10000 10001 10010 4

Number Systems Base Character Set 2 {0,1} 3 {0,1,2} 4 {0,1,2,3} 5 {0,1,2,3,4} 6 {0,1,2,3,4,5} 7 {0,1,2,3,4,5,6} 8 {0,1,2,3,4,5,6,7} 9 {0,1,2,3,4,5,6,7,8} 10 {0,1,2,3,4,5,6,7,8,9} 11 {0,1,2,3,4,5,6,7,8,9,A} 12 {0,1,2,3,4,5,6,7,8,9,A,B} 13 {0,1,2,3,4,5,6,7,8,9,A,B,C} 14 {0,1,2,3,4,5,6,7,8,9,A,B,C,D} 15 {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E} 16 {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F} Binary (Base 2) to Decimal q Sum up (bit*weight): 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 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 Binary Weights 10000011 2 = 10? 101001100 2 = 10? 5

Powers of 2 q Memorize! 2 10 2 9 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1024 512 256 128 64 32 16 8 4 2 1 2 80 2 70 2 60 2 50 2 40 2 30 2 20 2 10 Yotta Zetta Exa Peta Tera Giga Mega Kilo Y Z E P T G M K Octal (Base 8) to Decimal q Sum up (digit*weight): 8 4 8 3 8 2 8 1 8 0 4096 512 64 8 1 2 5 7 1 0 2 2 6 Octal weights 257 8 = 10? 10226 8 = 10? 6

Hexadecimal (Base 16) to Decimal q Sum up (hex digit*weight): 16 3 16 2 16 1 16 0 4096 256 16 1 A 2 B 1 4 A 6 Hexadecimal Weight A2B 16 = 10? 14A6 16 = 10? Coun3ng in Octal 0 = 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 = 12 = 13 = 14 = 15 = 16 = 17 = 18 = 00 0 00 1 00 2 00 3 00 4 00 5 00 6 00 7 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 20 0 21 0 22 7

Coun3ng in Hexadecimal 0 = 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 = 12 = 13 = 14 = 15 = 16 = 17 = 18 = 00 0 00 1 00 2 00 3 00 4 00 5 00 6 00 7 00 8 00 9 00 A 00 B 00 C 00 D 00 E 00 F 0 10 0 11 0 12 Decimal to Binary q 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 and so on 1 0 0 1???? 8

You Try It Powers of 2 1024 512 256 128 64 32 16 8 4 2 1 583 10 = 2? Hexadecimal Benefits q It is often convenient to write binary (base-2) numbers as hexadecimal (base-16) numbers instead. - fewer digits -- four bits per hex digit - less error prone -- easy to corrupt long string of 1 s and 0 s Binary Hex Decimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 Binary Hex Decimal 1000 8 8 1001 9 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15 9

Converting from Binary to Hexadecimal q Every four bits is a hex digit. - start grouping from right-hand side 011101010001111010011010111 3 A 8 F 4 D 7 This is not a new machine representation, just a convenient way to write the number. Exercises 1. 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. Convert the binary to hex: 10 0110 1110 1001 0100 1100 0101 1111 2 0010 0110 1110 1001 0100 1100 0101 1111 2 6 E 9 4 C 5 F 10

Exercises (contd.) 3. Convert from binary to octal: 10 111 010 100 110 001 011 111 2 010 111 010 100 110 001 011 111 2 7 2 4 6 1 3 7 Number Systems q The binary, hexadecimal (hex) and octal system share one common feature they are all based on powers of 2. q Each digit in the hex system is equivalent to a four- digit binary number and each digit in the octal system is equivalent to a 3- digit binary number. 11

Prac3ce Hex Decimal Binary 10 240 11111111 Binary Addi3on q Let s review decimal addi1on - From right to leg, 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 4 6 2 Carry 0 1 1 12

Binary Addi3on q From right to leg, we add each pair of bits 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10 q 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 0 6 1 2 1 0 1 1 + 0 0 1 Sum Carry Data Representa3on 13

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 Word-Oriented Memory Organization q Addresses Specify Byte Locations - Address of first byte in word - Addresses of successive words differ by 4 (32-bit) or 8 (64-bit) 32-bit 64-bit Words Words Bytes Addr. Addr = 0000?? Addr = 0004?? Addr = 0008?? Addr = 0012?? Addr = 0000?? Addr = 0008?? 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 14

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) Address vs. Value q Sometimes we want to deal with the address of a memory location, rather than the value it contains. address value q Adding a column of numbers. - R2 contains address of first location. - Read value, add to sum, and increment R2 until all numbers have been processed. q R2 is a pointer -- it contains the address of data we re interested in. R2 x3100 x3107 x2819 x0110 x0310 x0100 x1110 x11b1 x0019 x3100 x3101 x3102 x3103 x3104 x3105 x3106 x3107 15

Byte Ordering q How should bytes within multi-byte word be ordered in memory? q Conventions - Sun, Mac are Big Endian machines o Least significant byte has highest address - Alpha, PC 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 - Address 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 16

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 17

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 legmost 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? 18

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 Legmost 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. 19

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 Legmost 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 20

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 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 21

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 22

Number Representa3ons Review q Legmost 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 23

Ques3on q What value does 10011001 represent? ASCII American Standard Code for Informa3on Interchange 24

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 25

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/) 26

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 Bit- Level Operators (Common to C and Java) 27

Used in Networking Used in Encryption/ Decryption 28

Used in Compression Look at the DEFLATE algorithm, for instance - https://en.wikipedia.org/wiki/deflate - everything in bits, not bytes Overview of C bit-level operators ~ Bitwise NOT ( flips bits) & Bitwise AND ^ Bitwise XOR Bitwise OR << Bitwise left shift (shifts bits to left) >> Bitwise right shift (shifts bits to right) 29

Observation 1 Addition q 2 s comp. addition is just binary addition. - assume all integers have the same number of bits - for now, assume that sum fits in n-bit 2 s comp. representation 01101000 (104) 11110110 (-10) + 11110000 (-16) + (-9) 01011000 (88) (-19) Assuming 8-bit 2 s complement numbers. Observation 2 Sign Extension q To add two numbers, we must represent them with the same number of bits. q If we just pad with zeroes on the left: 4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 00001100 (12, not -4; NOT GOOD!) q Instead, replicate the MS bit -- the sign bit: 4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 11111100 (still -4) 30

Bit-Level Operations in C q q Operations &,, ~, ^ available in C - Apply to any integral data type o long, int, short, char - View arguments as bit vectors - Arguments applied bit-wise Examples (char data type) - ~0x41 --> 0xBE ~01000001 2 --> 10111110 2 - ~0x00 --> 0xFF ~00000000 2 --> 11111111 2-0x69 & 0x55 --> 0x41 01101001 2 & 01010101 2 --> 01000001 2-0x69 0x55 --> 0x7D 01101001 2 01010101 2 --> 01111101 2 Bitwise Operator: NOT (~) q NOT (~) is the same as one s complement - Turns 0 to 1, and 1 to 0 q Examples: - Assume that x is an integer on 8 bits. Set x = 1. What is the value of ~x? 0 1 ~x - Set the least significant bit of y to 0: y = y & ; 31

Bitwise Operators: AND (&) and OR (I) Bitwise AND (&) Bitwise OR ( ) & 0 1 0 1 0 1 0 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 Bitwise Operators: AND (&) and OR (I) Assume that x is an integer variable. q Determine the least significant bit of x: int bit = x & ; q Set the least significant bit of x to 1: x = x ; 32

Bitwise Operator: XOR q XOR (^) - 0 if both bits are the same - 1 if the two bits are different ^ 0 1 0 1 q For an integer x, what is the value of x^x Relations Between Operations q DeMorgan s Laws - Express & in terms of, and vice-versa o A & B = ~(~A ~B) A and B are true if and only if neither A nor B is false o A B = ~(~A & ~B) A or B are true if and only if A and B are not both false q Exclusive-Or using Inclusive Or o A ^ B = (~A & B) (A & ~B) Exactly one of A and B is true o A ^ B = (A B) & ~(A & B) Either A is true, or B is true, but not both 33

Bitwise Operator: Le` Shi` q Leg shig: x << y (same as mul1ply by 2 y ) - Shig bit- vector x leg y posi1on - Fill blanks with 0 53 53<<2 0 0 1 1 0 1 0 1 Bitwise Operator: Right Shi` q Right shig: x >> y (same as divide by 2 y ) - Shig bit- vector x right y posi1ons - Fill in blanks with sign bit (this is called arithme/c shi0) 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 Note: Java also has the unsigned shift operator >>> Not available in C. 34

Bitwise Operators q Print all the bits of a character from right to leg (star1ng with the least significant bit): char c = 0xB5; 0 0 1 1 0 1 0 1 void ReversePrintBits(char c) { /* add code here */ } Bitwise Operators q Print all the bits of a character from leg to right (star1ng with the most significant bit): char c = 0xB5; 0 0 1 1 0 1 0 1 void PrintBits(char c) { /* add code here */ } 35

Bitmasks q Used to change or query one or more bits in a variable. q The bitmask indicates which bits are to be affected. q Common opera1ons: - Set one or more bits (set to 1) - Clear one or more bits (set to zero) - Read one or more bits q Examples: - Set bit 2 of x (bit 0 is least significant): x = x - Clear bit 3 of x: x = x - Read bit 4 of x: bit = x Bitmasks q Set the least significant byte of x to FF: x = x ; q Clear the least significant byte of x: x = x ; q Read the least significant byte of x: byte = x ; 36

Bitmasks q Set to 1 bits 2, 4 and 7 of x (0 is least significant): x = x ; q Clear bits 3, 4 and 5 of x: x = x ; Contrast: Logical Operators 37

Familiar Operators (common to C, Java) Category Operators Arithmetic ++expr --expr expr++ expr-- expr1*expr2 expr1/expr2 expr1%expr2 expr1+expr2 expr1-expr2 Assignment expr1=expr2 expr1*=expr2 expr1/=expr2 expr1%=expr2 expr1+=expr2 expr1-=expr2 Relational expr1<expr2 expr1<=expr2 expr1>expr2 expr1>=expr2 expr1==expr2 expr1!=expr2 Logical!expr expr1&&expr2 expr1 expr2 Function Call Cast Conditional func(paramlist) (type)expr expr1?expr2:expr3 Logical Operators && (Logical AND), (Logical OR),! (Logical NOT) q Always return 0 or 1 q View 0 as False q Anything nonzero as True q Examples (char data type) -!0x41 --> -!0x00 --> -!!0x41 --> - 0x69 && 0x55 --> - 0x69 0x55 --> 38

What is the Output? int a = 0x43, b = 0x21; printf("a b = %x\n", a b); Printf("a b = %x\n", a b); printf("a & b = %x\n", a & b); printf("a && b = %x\n", a && b); Mental Exercise /* * isnotequal - return 0 if x == y, and 1 otherwise * Examples: isnotequal(5,5) = 0, isnotequal(4,5) = 1 */ int isnotequal (int x, int y) { return ; } 39

Limits of the Machine: How much memory space for data? Storage Units 1 bit = smallest unit of memory 1 byte = 8 bits 4 bytes = 1 word (system dependent) 40

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 Addr. Addr = 0000?? Addr = 0004?? Addr = 0008?? Addr = 0012?? Addr = 0000?? Addr = 0008?? 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 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 */ 41

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

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 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); 43

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 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 44

Try It Out q C code: char a = 0xFF; unsigned char b = 0xFF; printf("a = %d\n", a); printf("b = %d\n", b); 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; 45

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 */ 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, binary opera1ons, sign extension, limits of machine, overflow, cast conversions 46