Arithmetic and Bitwise Operations on Binary Data

Similar documents
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.

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

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

Manipulating Integers

Bits, Bytes, and Integers

Lecture 5-6: Bits, Bytes, and Integers

Systems Programming and Computer Architecture ( )

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Bits, Bytes, and Integers Part 2

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

Bits, Bytes, and Integers August 26, 2009

Representing and Manipulating Integers. Jo, Heeseung

Bits, Bytes and Integers Part 1

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

Bits, Bytes and Integers

Computer Systems CEN591(502) Fall 2011

Integer Encoding and Manipulation

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

CSCI2467: Systems Programming Concepts

Bits, Bytes, and Integers

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

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

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

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

Integers Sep 3, 2002

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

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

Bits and Bytes January 13, 2005

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

Foundations of Computer Systems

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

CS 270: Computer Organization. Integer Arithmetic Operations

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 &,, ~, ^, <<, >>

Computer Organization: A Programmer's Perspective

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

CS429: Computer Organization and Architecture

Data III & Integers I

Bitwise Data Manipulation. Bitwise operations More on integers

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

Data III & Integers I

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Data III & Integers I

Integers II. CSE 351 Autumn 2018

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Chapter 4. Operations on Data

JAVA OPERATORS GENERAL

Hardware: Logical View

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

Data Representation Floating Point

Floating Point Numbers

Floating Point Numbers

Data Representation Floating Point

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

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

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

CS429: Computer Organization and Architecture

COMP2611: Computer Organization. Data Representation

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

CS33 Project Gear Up. Data

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

CS61B Lecture #14: Integers

Computer Organization & Systems Exam I Example Questions

7/25/2016. Example: Addition of Unsigned Bit Patterns. ECE 120: Introduction to Computing. Adding Two Non-Negative Patterns Can Overflow

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

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

Representing and Manipulating Integers Part I

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

Integer Representation

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

The Arithmetic Operators

CS 261 Fall Mike Lam, Professor Integer Encodings

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

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

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

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

Data Representation Floating Point

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

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

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

AGENDA Binary Operations CS 3330 Samira Khan

CSCI 2212: Intermediate Programming / C Chapter 15

SIGNED AND UNSIGNED SYSTEMS

Bits, Words, and Integers

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

REMEMBER TO REGISTER FOR THE EXAM.

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

EE292: Fundamentals of ECE

COMP 122/L Lecture 2. Kyle Dewey

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

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Floating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides

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

Operators. Java operators are classified into three categories:

Binary Representations and Arithmetic

Transcription:

Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides adapted from Bryant & O Hallaron s slides 1

Boolean Algebra Developed by George Boole in 19th Century Algebraic representation of logic Encode True as 1 and False as And Or A&B = 1 when both A=1 and B=1 A B = 1 when either A=1 or B=1 Not ~A = 1 when A= Exclusive-Or (Xor) A^B = 1 when either A=1 or B=1, but not both 2

General Boolean Algebras Operate on Bit Vectors Operations applied bitwise Bitwise-AND operator: Bitwise-NOR operator: Bitwise-XOR operator: Bitwise-NOT operator: ~ 1111 & 1111 11 & ^ 1111 1111 111111 1111 ^ 1111 1111 ~ 1111 11 111111 1111 1111 All of the Properties of Boolean Algebra Apply 4

Bit-Level Operations 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 Examples (char data type): in hexadecimal in binary ~x41 xbe // ~112 1111112 ~x xff // ~2 111111112 x69 & x55 x41 // 11112 & 11112 112 x69 x55 x7d // 11112 11112 1111112 6

Contrast: Logic Operations in C Contrast to Logical Operators &&,,! View as False Anything nonzero as True Always return or 1 Early termination Examples (char data type):!x41 x!x x1!!x41 x1 x69 && x55 x1 x69 x55 x1 p && *p // avoids null pointer access 7

Shift Operations Left Shift: x << y Shift bit-vector x left y positions Throw away extra bits on left Fill with s on right Right Shift: x >> y Shift bit-vector x right y positions Throw away extra bits on right Logical shift Fill with s on left Arithmetic shift Replicate most significant bit on right Undefined Behavior Shift amount < or word size Argument x << 3 Log. >> 2 Arith. >> 2 Argument x << 3 Log. >> 2 Arith. >> 2 111 1 11 11 111 1 11 1111 8

Bitwise-NOT: One s Complement Bitwise-NOT operation: ~ Bitwise-NOT of x is ~x Flip all bits of x to compute ~x flip each 1 to flip each to 1 Complement Given x == 11111 x: 1 1 1 1 1 Flip bits (one s complement): ~x: 1 1 1 9

Negation: Two s Complement Negate a number by taking 2 s Complement Flip bits (one s complement) and add 1 ~x + 1 == -x Negation (Two s Complement): Given x == 11111 x: 1 1 1 1 1 Flip bits (one s complement): Add 1: ~x: 1 1 1 + 1 -x: 1 1 1 1 1

Complement & Increment Examples x = 15213 Decimal Hex Binary x 15213 3B 6D 11111 11111 ~x -15214 C4 92 111 111 ~x+1-15213 C4 93 111 1111 y -15213 C4 93 111 1111 x = Decimal Hex Binary ~ -1 FF FF 11111111 11111111 ~+1 11

Unsigned Addition Operands: w bits True Sum: w+1 bits Discard Carry: w bits u + v u + v Addition Operation Carry output dropped at end of addition Valid ONLY if true sum is within w-bit range Example #1: 1 1 1 1 1 98 1 + 1 1 1 74 1 1 1 1 1 172 1 Valid in 8-bit unsigned range 12

Unsigned Addition Example #2: 1 + 1 Example #3: 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 22 1 1 1 1 56 1 Not Validin 8-bit unsigned range (312 is > 255) + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 182 1 1 1 1 59978 1 1 1 1 1 4524 1 Not Valid in 16-bit unsigned range (76 is > 65535) 13

Visualizing (Mathematical) Integer Addition Integer Addition 4-bit integers u, v Compute true sum Values increase linearly with u and v Forms planar surface u v 14

Visualizing Unsigned Addition Wraps Around If true sum 2 w At most once Overflow True Sum 2 w+1 Overflow 16 14 12 1 2 w Modular Sum 8 6 4 2 u 2 4 6 8 1 12 14 2 4 6 8 1 12 v 14 15

Visualizing Signed Addition NegOver Values 4-bit two s comp. Range from -8 to +7 Wraps Around If sum 2 w 1 Becomes negative At most once If sum < 2 w 1 Becomes positive At most once 8 6 4 2-2 -4-6 -8-8 -6-4 -2 u 2 4 6-8 -6-4 -2 6 4 2 v PosOver 16

Signed Addition Example #1: + Example #2: 1 1 1 1 1 98 1 1 1 1 74 1 1 1 1 1-84 1 Saint Louis University Note: Same bytes as for Ex #1 and Ex #2 in unsigned integer addition, but now interpreted as 8-bit signed integers Not Validin 8-bit signed range (172 > 127) + 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1-54 1 1 1 1 56 1 Valid in 8-bit signed range (-128 < 56 < 127) 17

Arithmetic: Basic Rules Unsigned ints, 2 s complement intsare isomorphic rings: isomorphism = casting Left shift Unsigned/signed: multiplication by 2 k Always logical shift Right shift Unsigned: logical shift, div (division + round to zero) by 2 k Signed: arithmetic shift Positive numbers: div (division + round to zero) by 2 k Negative numbers: div (division + round away from zero) by 2 k Use biasing to fix 18

Integer C Puzzles Initialization intx = foo(); inty = bar(); unsigned ux= x; unsigned uy= y; x < ((x*2) < ) ux >= x & 7 == 7 (x<<3) < ux > -1 x > y -x < -y x * x >= x > && y > x + y > x >= -x <= x <= -x >= (x -x)>>31 == -1 ux >> 3 == ux/8 x >> 3 == x/8 x & (x-1)!= 19