CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Similar documents
A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

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

Beginning C Programming for Engineers

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

CS 241 Data Organization Binary

COMP2611: Computer Organization. Data Representation

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Lecture 13 Bit Operations

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

The Arithmetic Operators

CS 64 Week 0 Lecture 1. Kyle Dewey

JAVA OPERATORS GENERAL

Computer Organization & Systems Exam I Example Questions

Binary Representations and Arithmetic

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

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

Lecture Notes on Ints

Lecture 17 Bit Operations

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

CS 107 Lecture 2: Bits and Bytes (continued)

Integers II. CSE 351 Autumn Instructor: Justin Hsia

LAB A Translating Data to Binary

Survey. Motivation 29.5 / 40 class is required

Bits, Bytes and Integers

Arithmetic and Bitwise Operations on Binary Data

CSCI 2212: Intermediate Programming / C Chapter 15

The Design of C: A Rational Reconstruction

Arithmetic Processing

The Design of C: A Rational Reconstruction"

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

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

The type of all data used in a C (or C++) program must be specified

Inf2C - Computer Systems Lecture 2 Data Representation

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

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

Integers II. CSE 351 Autumn 2018

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

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

Description Hex M E V smallest value > largest denormalized negative infinity number with hex representation 3BB0 ---

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

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

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

Number Systems and Number Representation

The type of all data used in a C++ program must be specified

Arithmetic Operators. Portability: Printing Numbers

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

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

Solutions - Homework 2 (Due date: October 4 5:30 pm) Presentation and clarity are very important! Show your procedure!

Bits, Words, and Integers

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

Work relative to other classes

Number Representations

What Is It? Instruction Register Address Register Data Register

CS2141 Software Development using C/C++ C++ Basics

Unsigned Binary Integers

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Unsigned Binary Integers

Number systems and binary

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

Data Representation 1

SECTION II: LANGUAGE BASICS

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

Learning the Binary System

CSE 303 Lecture 18. Bitwise operations. reading: Programming in C Ch. 12. slides created by Marty Stepp

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Arithmetic Operations

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

COMP Primitive and Class Types. Yi Hong May 14, 2015

Number Systems CHAPTER Positional Number Systems

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

SIGNED AND UNSIGNED SYSTEMS

Midterm Exam Review Slides

T02 Tutorial Slides for Week 2

The C Programming Language Guide for the Robot Course work Module

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

SU 2017 May 18/23 LAB 3 Bitwise operations, Program structures, Functions (pass-by-value), local vs. global variables. Debuggers

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

The Design of C: A Rational Reconstruction

Number System. Introduction. Decimal Numbers

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

Arithmetic and Bitwise Operations on Binary Data

Applied Computer Programming

Number Systems. Both numbers are positive

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Programming. Elementary Concepts

Topic Notes: Bits and Bytes and Numbers

COMP2121: Microprocessors and Interfacing. Number Systems

CS61B Lecture #14: Integers

Embedded Systems - FS 2018

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

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

ME 461 C review Session Fall 2009 S. Keres

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Transcription:

CSE 351: The Hardware/Software Interface Section 2 Integer representations, two s complement, and bitwise operators

Integer representations In addition to decimal notation, it s important to be able to understand binary and hexadecimal representations of integers Decimal: 3735928559 No prefix, just the number Binary: 0b11011110101011011011111011101111 0b prefix denotes binary notation Hexadecimal: 0xDEADBEEF 0x prefix denotes hexadecimal notation Which notation is the most compact of the three? Why use one over another? 1/17/13 2

Binary scale Each digit in binary notation is either 0b0 (zero) or 0b1 (one) To convert from (unsigned) binary to decimal notation, take the sum of the nth digit multiplied by 2 n-1 As an example, 0b1101 = 1 * 2 3 + 1 * 2 2 + 0 * 2 1 + 1 * 2 0 = 8 + 4 + 0 + 1 = 13 1/17/13 3

Binary scale To convert from decimal to binary, use a combination of division and modulus to get each digit, tracking the remainder As an example, let s convert 11 to binary (11 / 2 0 ) % 2 = 1, so the first digit is 0b1. Remainder is 11-1 * 2 0 = 10 (10 / 2 1 ) % 2 = 5 % 2 = 1, so the second digit is 0b1. Remainder is 10-1 * 2 1 = 8 (8 / 2 2 ) % 2 = 4 % 2 = 0, so the third digit is 0b0. Remainder is 8-0 * 2 2 = 8 (8 / 2 3 ) % 2 = 1 % 2 = 1, so the fourth digit is 0b1 Finally, we have that 11 is 0b1011 in binary 1/17/13 4

Hexadecimal scale Each digit ranges in value from 0x0 (zero) to 0xF (fifteen) A => ten, B => eleven, C => twelve, D => thirteen, E => fourteen, F => fifteen To convert from (unsigned) hexadecimal to decimal notation, take the sum of the nth digit multiplied by 16 n-1 As an example, 0xACE = 0xA * 16 2 + 0xC * 16 1 + 0xE * 16 0 = 10 * 256 + 12 * 16 + 14 = 2766 1/17/13 5

Hexadecimal scale The decimal to hexadecimal conversion is the same process as decimal to binary except with 2 instead of 16 As an example, let s convert 3254 to hexadecimal (3254 / 16 0 ) % 16 = 6, so first digit is 0x6. Remainder is 3254-0x6 * 16 0 = 3248 (3248 / 16 1 ) % 16 = 203 % 16 = 11 = 0xB, so second digit is 0xB. Remainder is 3248-0xB * 16 1 = 3248-176 = 3072 (3072 / 16 2 ) % 16 = 12 % 16 = 12 = 0xC, so third digit is 0xC Finally, we have that 3254 is 0xCB6 in hexadecimal If we were to write a program to convert from decimal to binary or to hexadecimal, how could we compute the nth digit efficiently using bitwise operators and modulus (%)? 1/17/13 6

Two s complement review In class, we established that two s complement is a nice format for representing signed integers for a couple different reasons. What were they? 1 0 2 3 1111 1110 0000 0001 + 1 + 2 1101 0010 4 1100 + 3 0011 5 1011 1010 6 1001 0100 0101 0110 7 1000 0111 + 6 1/17/13 8 + 7 7 + 5 + 4

Two s complement review Let s say that we want to encode -5 in binary using two s complement form and four bits With four bits, the highest bit has a negative weight of 2 3, so 0b1000 = -8-5 = -8 + 2 + 1 = 1 * -2 3 + 0 * 2 2 + 1 * 2 1 + 1 * 2 0 = 10b1011 5 = 4 + 1 = 0 * -2 3 + 1 * 2 2 + 0 * 2 1 + 1 * 2 0 = 0b0101 1101 1100 1011 1010 1110 1001 1111 0000 0001 0110 0010 0011 0100 0101 7 1000 0111 + 6 1/17/13 8 + 7 8 4 3 5 6 2 1 0 + 1 + 2 + 5 + 3 + 4

Operator review ~ is arithmetic not (flip all bits) Example: ~0b1010 = 0b0101! is logical not (1 if 0b0, else 0) Example:!0b100 = 0,!0b0 = 1 & is bitwise and Example: 0b101 & 0b110 = 0b100 is bitwise or Example: 0b101 0b100 = 0b101 >> is bitwise right shift Example: 0b1010 >> 1 = 0b1101, 0b0101 >> 1 = 0b0010 << is bitwise left shift Example: 0b1010 << 1 = 0b0100, 0b1000 << 1 = 0b0000 1/17/13 9

Operator uses Can express negation in terms of arithmetic not and addition For example, ~4 + 1 = ~0b0100 + 1 = 0b1011 + 1 = -5 + 1 = -4 Can use shifting, bitwise and, and logical not to detect if a particular bit is set As a simple example,!!(x & (0x1 << 1)) evaluates to 1 if the second bit it set in x and 0 otherwise Useful for checking if a value is negative Can implement ternaries (x =? : ) using bitwise and, bitwise or, and arithmetic not This has wide-ranging applications in lab 1 1/17/13 10

Bitwise operators in practice Is what we re learning ever useful in practice? Thankfully (or not, depending on how you look at it), it is Setting bits in permission strings For example, to choose the 1/17/13 11

Packing and unpacking Let s say that you have values x, y, and z that take 3, 4, and 1 bit to represent, respectively Is there a way to store these three values using only eight bits? In C, we can define a struct that specifies the width in bits of each value though the compiler will add padding to make the struct a certain size if you don t do so yourself In Java, there are no structs, and we have to use bitwise operators 12/9/10 12

Packing and unpacking (C) #include <stdio.h> typedef struct { int x : 3; int y : 4; int z : 1; int padding : 24; } Flags; int main(int argc, char* argv[]) { Flags flags = {3, 8, 1, 0x8fffff}; printf("sizeof(flags) is %ju and it stores 0x%x\n", sizeof(flags), *(int*) &flags); } return 0; 12/9/10 13

Packing and unpacking (Java) // Pack some values into a byte byte bitvalue = 0; bitvalue = 3; bitvalue = 8 << 3; bitvalue = 1 << 7; // Unpack the values from the byte byte x = bitvalue & 0x7; byte y = bitvalue & 0x78; byte z = bitvalue & 0x80; // Alternatively, we could have shifted a particular // mask instead, e.g. (0x1 << 7) instead of 0x80 12/9/10 14

Lab 1 hints Decompose each problem into smaller problems If you are stuck on how to solve something, write it as a combination of functions and boolean logic Over time, replace each function or boolean operator with a combination of permitted operators Hint for detecting overflow: what is the sign of the integer produced by adding TMax to a positive value? What about when adding negative numbers? Hint for counting bits: consider multiple bits at once. 40 operations isn t enough to check each individually 1/17/13 15