Bitwise Data Manipulation. Bitwise operations More on integers

Similar documents
Integer Representation

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

Data III & Integers I

Data III & Integers I

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn 2018

Data III & Integers I

Arithmetic and Bitwise Operations on Binary Data

Systems Programming and Computer Architecture ( )

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.

Bits, Bytes and Integers Part 1

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

Integer Encoding and Manipulation

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Bits, Bytes and Integers

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

Representing and Manipulating Integers. Jo, Heeseung

CSCI2467: Systems Programming Concepts

Bits, Bytes, and Integers

Arithmetic and Bitwise Operations on Binary Data

Lecture 5-6: Bits, Bytes, and Integers

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

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

BITS, BYTES, AND INTEGERS

Representing and Manipulating Integers Part I

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

Integers Sep 3, 2002

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

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

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

Hardware: Logical View

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

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

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?

LAB A Translating Data to Binary

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

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

CS429: Computer Organization and Architecture

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

CS61B Lecture #14: Integers

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Computer Systems CEN591(502) Fall 2011

JAVA OPERATORS GENERAL

Bits and Bytes January 13, 2005

Arithmetic Operators. Portability: Printing Numbers

Informatics Ingeniería en Electrónica y Automática Industrial

COMP2611: Computer Organization. Data Representation

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

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

AGENDA Binary Operations CS 3330 Samira Khan

Integer Representation

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

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

Foundations of Computer Systems

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Manipulating Integers

In case you didn t get it (or you were too lazy to check)

Representing Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

Representing Integers

Chapter 3: Operators, Expressions and Type Conversion

Computer Systems C S Cynthia Lee

Bits, Bytes, and Integers Part 2

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

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

UNIT 3 OPERATORS. [Marks- 12]

Arithmetic Operations

Computer Organization & Systems Exam I Example Questions

Operators. Java operators are classified into three categories:

CS 107 Lecture 3: Integer Representations

CS 253. January 14, 2017

Bits, Bytes, and Integers August 26, 2009

CT 229. Java Syntax 26/09/2006 CT229

More on Images and Matlab

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Where we are going (today)

Arithmetic-logic units

Computer Organization: A Programmer's Perspective

The Design of C: A Rational Reconstruction

Information Science 1

Bitwise Operator and Typecasting

1.3b Type Conversion

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

Expressions and Casting

The Design of C: A Rational Reconstruction"

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Computers Programming Course 6. Iulian Năstac

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

Unsigned Binary Integers

Unsigned Binary Integers

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

Transcription:

Bitwise Data Manipulation Bitwise operations More on integers

bitwise operators ex Bitwise operators on fixed-width bit vectors. AND & OR XOR ^ NOT ~ 01101001 & 01010101 01000001 01101001 01010101 01101001 ^ 01010101 ~ 01010101 01010101 ^ 01010101 Laws of Boolean algebra apply bitwise. e.g., DeMorgan s Law: ~(A B) = ~A & ~B 2

Aside: sets as bit vectors ex Representation: n-bit vector gives subset of {0,, n 1}. a i = 1 i Î A 01101001 { 0, 3, 5, 6 } 76543210 01010101 { 0, 2, 4, 6 } 76543210 Bitwise Operations Set Operations? & 01000001 { 0, 6 } Intersection 01111101 { 0, 2, 3, 4, 5, 6 } Union ^ 00111100 { 2, 3, 4, 5 } Symmetric difference ~ 10101010 { 1, 3, 5, 7 } Complement 3

bitwise operators in C ex & ^ ~ apply to any integral data type long, int, short, char, unsigned Examples (char) ~0x41 = ~0x00 = 0x69 & 0x55 = 0x69 0x55 = Many bit-twiddling puzzles in upcoming assignment 4

logical operations in C ex &&! apply to any "integral" data type long, int, short, char, unsigned 0 is false nonzero is true result always 0 or 1 early termination a.k.a. short-circuit evaluation Examples (char)!0x41 =!0x00 =!!0x41 = 0x69 && 0x55 = 0x69 0x55 = 5

Encode playing cards. 52 cards in 4 suits How do we encode suits, face cards? What operations should be easy to implement? Get and compare rank Get and compare suit 6

Two possible representations 52 cards 52 bits with bit corresponding to card set to 1 52 bits in 2 x 32-bit words One-hot encoding Hard to compare values and suits independently Not space efficient 4 bits for suit, 13 bits for card value 17 bits with two set to 1 Pair of one-hot encoded values Easier to compare suits and values independently Smaller, but still not space efficient 7

Two better representations Binary encoding of all 52 cards only 6 bits needed Number cards uniquely from 0 Smaller than one-hot encodings. Hard to compare value and suit low-order 6 bits of a byte Binary encoding of suit (2 bits) and value (4 bits) separately Number each suit uniquely Number each value uniquely Still small Easy suit, value comparisons suit value 8

Compare Card Suits mask: a bit vector that, when bitwise ANDed with another bit vector v, turns all but the bits of interest in v to 0 0 0 1 1 0 0 0 0 #define SUIT_MASK 0x30 suit value int samesuit(char card1, char card2) { return!((card1 & SUIT_MASK) ^ (card2 & SUIT_MASK)); } //same as (card1 & SUIT_MASK) == (card2 & SUIT_MASK); char hand[5]; // represents a 5-card hand char card1, card2; // two cards to compare... if ( samesuit(hand[0], hand[1]) ) {... } 9

Compare Card Values ex mask: a bit vector that, when bitwise ANDed with another bit vector v, turns all but the bits of interest in v to 0 #define VALUE_MASK suit value int greatervalue(char card1, char card2) { } char hand[5]; // represents a 5-card hand char card1, card2; // two cards to compare... if ( greatervalue(hand[0], hand[1]) ) {... } 10

Bit shifting x 1 0 0 1 1 0 0 1 x << 2 1 0 0 1 1 0 0 1 0 0 logical shift left 2 lose bits on left fill with zeroes on right 1 0 0 1 1 0 0 1 x fill with zeroes on left logical shift right 2 arithmetic shift right 2 fill with copies of MSB on left 0 0 1 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 1 x >> 2 lose bits on right x >> 2 11

unsigned shifting and arithmetic unsigned x = 27; y = x << 2; y == 108 0 0 0 1 1 0 1 1 0 0 0 1 1 0 1 1 0 0 logical shift left x*2 n mod 2 w x/2 n logical shift right 1 1 1 0 1 1 0 1 0 0 1 1 1 0 1 1 0 1 unsigned x = 237; y = x >> 2; y == 59 12

two's complement shifting and arithmetic signed x = -101; y = x << 2; y == 108 1 0 0 1 1 0 1 1 1 0 0 1 1 0 1 1 0 0 f( x*2 n mod 2 w ) logical shift left x/2 n arithmetic shift right 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 1 0 1 signed x = -19; y = x >> 2; y == -5 13

shift-and-add ex Available operations x << k x + y implements x * 2 k Implement y = x * 24 using only <<, +, and integer literals 14

Shift gotchas!!! Logical or arithmetic shift right: how do we tell? C: compiler chooses Usually based on type: rain check! Java: >> is arithmetic, >>> is logical Shift an n-bit type by at least 0 and no more than n-1. C: other shift distances are undefined. anything could happen Java: shift distance is used modulo number of bits in shifted type Given int x: x << 34 == x << 2

Shift and Mask: extract a bit field ex Write C code: extract 2 nd most significant byte from a 32-bit integer. given x = should return: 01100001 01100010 01100011 01100100 00000000 00000000 00000000 01100010 All other bits are zero. Desired bits in least significant byte. 16

What does this function compute? ex unsigned puzzle(unsigned x, unsigned y) { unsigned result = 0; for (unsigned i = 0; i < 32; i++){ if (y & (1 << i)) { result = result + (x << i); } } return result; } 17

multiplication 2 0010 1 0 x 3 6 x 0011 00000100 2 1111 3 1110 1101 4 1100 0000 0001 + 1 0010 0011 + 2 + 3-2 x 2-4 1110 x 0010 11111100 5 1011 1010 6 1001 0100 0101 0110 7 1000 0111 + 6 8 + 7 + 5 + 4 Modular Arithmetic 18

multiplication 5 x 4 20 4-3 x 7-21 -2 0101 x 0100 00010100 1101 x 0111 11101011 1 0 2 1111 0000 + 1 3 1110 0001 + 2 1101 0010 4 1100 0011 + 3 5 1011 1010 6 1001 0100 0101 0110 7 1000 0111 + 6 8 + 7 + 5 + 4 Modular Arithmetic 19

multiplication 5 x 5 25-7 -2 x 6-12 4 0101 x 0101 00011001 1110 x 0110 11110100 1 0 2 1111 0000 + 1 3 1110 0001 + 2 1101 0010 4 1100 0011 + 3 5 1011 1010 6 1001 0100 0101 0110 7 1000 0111 + 6 8 + 7 + 5 + 4 Modular Arithmetic 20

Convert/cast signed number to larger type. 0 0 0 0 0 0 1 0 8-bit 2 0 0 0 0 0 0 1 0 16-bit 2 1 1 1 1 1 1 0 0 8-bit -4 1 1 1 1 1 1 0 0 16-bit -4 Rule/name? 21

Casting Integers in C!!! Number literals: 37 is signed, 37U is unsigned Integer Casting: bits unchanged, just reinterpreted. Explicit casting: int tx = (int) 73U; // still 73 unsigned uy = (unsigned) -4; // big positive # Implicit casting: tx = ux; uy = ty; void foo(int z) {... } foo(ux); Actually does // tx = (int)ux; // uy = (unsigned)ty; // foo((int)ux); if (tx < ux)... // if ((unsigned)tx < ux)... 23

More Implicit Casting in C!!! If you mix unsigned and signed in a single expression, then signed values are implicitly cast to unsigned. How are the argument bits interpreted? Argument 1 Op Argument 2 Type Result 0 == 0U unsigned 1-1 < 0 signed 1-1 < 0U unsigned 0 2147483647 < -2147483648 2147483647U < -2147483648-1 < -2 (unsigned)-1 < -2 2147483647 < 2147483648U 2147483647 < (int)2147483648u Note: T min = -2,147,483,648 T max = 2,147,483,647 24