Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Advertisements

NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.c: Logical Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Computer Organization & Assembly Language
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Assembly Language and Computer Architecture Using C++ and Java
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Assembly Language and Computer Architecture Using C++ and Java
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
+ CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Binary numbers and arithmetic. ADDITION Addition (decimal)
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
Ch. 7 Logic, Shift and Rotate instr.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Sahar Mosleh California State University San MarcosPage 1 CPU Flags and Boolean Instructions.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.
Logical and Bit Operations Chapter 9 S. Dandamudi.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Comparing and Branching ifs and loops Part B. LOOPS.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Programming Principles Operators and Expressions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
Microprocessor & Assembly Language
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Bitwise and Logical Manipulations Assembly Language Programming University of Akron Dr. Tim Margush.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Data Transfers, Addressing, and Arithmetic
Assignment and Arithmetic expressions
Bit Operations Horton pp
Chapter 3 Bit Operations
The FLAGS Register An x bit means an unidentified value 9/12/2018
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly Language Programming Part 2
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
CS-401 Computer Architecture & Assembly Language Programming
Shift & Rotate Instructions)
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Shift & Rotate Instructions)
Homework Homework Continue Reading K&R Chapter 2 Questions?
Comp Org & Assembly Lang
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Bitwise operators.
Microprocessor and Assembly Language
Computer Organization and Assembly Language
Shift and Rotate Instructions.
Bit Manipulations CS212.
Ch. 5 – Intel 8086 – study details from Yu & Marut
Bit Operations Horton pp
Presentation transcript:

Bits and Bytes

BITWISE OPERATORS

Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.

Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Also conditional operators: &&, || – Difference between & and &&. – Differences between | and ||. – How can we demonstrate this?

RECALL BITWISE (INTEGER) OPERATORS

Bitwise integer operators HLL such as Java and C/C++ support many bitwise integer operators. Java: – bitwise:&, ^, |, -, ~ – shift: >, >>> (Note: C/C++ supports all of the above except >>>.)

Bitwise integer operators 1.Note: In Java and C/C++ as in Assembler, the bitwise operators can be applied to any of the integer data types (char, short, int, and long). 2.Note: All of the following examples employ 8-bit (byte) or 16-bit (short) integers but the ideas can and are extended to other integer data sizes. 3.Note: All of the following examples represent numbers in hex and binary for convenience. These rules apply to decimal numbers as well.

Bitwise & (and)

Bitwise | (or)

Bitwise ^ (xor)

~ (1’s complement) Given 0x3a, what is ~0x3a? Given 0x0022, what is ~0x0022?

- (2’s complement) Given 0x3a, what is -0x3a? Given 0x0022, what is -0x0022?

Shift operators 1.Left shift: A << B 2.Signed right shift: A >> B(sign extension) 3.Unsigned right shift: A >>> B(zero extension) A = value to be shifted B = shift distance

Shift operators Left shift:A << B

Shift operators Signed right shift:A >> B(sign extension)

Shift operators Unsigned right shift:A >>> B(zero extension)

Examples 10 << 1 7 << 3 -1 << 2

Examples 10 << << << 2-4

Examples 10 >> 1 27 >> >> 2

Examples 10 >> >> 1 = >> >> 3 = >> >> 2 = What is ? = = 13 So = -13 Preserves sign.

Examples -50 >>> 2 0xff >>> 4

Examples -50 >>> >>> 2 = xff >>> >>> 4 = Does not preserve sign.

Examples byte b = ~12; flags = flags & ~0xf; 10 & 7 if ((flags & 0xf) != 0)

Examples 10 | 7 flags = flags | 0xf; flags |= 0xa; 10 ^ 7 ~0x97

Examples int bits = 1; bits = bits << 1; bits = bits << 2; bits = bits >> 3;

Examples int b1 = 0x65; int b2 = 0xaf; int x = b1 & b2; int y = b1 ^ b2; int z = b1 | b2;

Problem 1 How can we determine if an int is odd or even?

Problem 2 Using bitwise operators, write a program in Java that converts an int from its current endian-ness to the other.

Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0;// What does this indicate? // How do I indicate that student #7 took the quiz?

Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0;// indicate that no one took the quiz quiz1 |= 1 << 7;// indicate that student #7 took the quiz // How do I indicate that student #4 took the quiz?

Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0;// indicate that no one took the quiz quiz1 |= 1 << 7;// indicate that student #7 took the quiz quiz1 |= 1 << 4;// indicate that student #4 took the quiz // Oops! Student #7 didn’t take the quiz. Turn it off.

Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0;// indicate that no one took the quiz quiz1 |= 1 << 7;// indicate that student #7 took the quiz quiz1 |= 1 << 4;// indicate that student #4 took the quiz quiz1 &= ~(1 << 7);// Oops! Student #7 didn’t take the quiz. Turn it off.

Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0;// indicate that no one took the quiz quiz1 |= 1 << 7;// indicate that student #7 took the quiz quiz1 |= 1 << 4;// indicate that student #4 took the quiz quiz1 &= ~(1 << 7);// Oops! Student #7 didn’t take the quiz. Turn it off. int quiz2 = 0;// let’s give another quiz! …// indicate students that took quiz 2 // How can we determine if student #4 took both?

Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0;// indicate that no one took the quiz quiz1 |= 1 << 7;// indicate that student #7 took the quiz quiz1 |= 1 << 4;// indicate that student #4 took the quiz quiz1 &= ~(1 << 7);// Oops! Student #7 didn’t take the quiz. Turn it off. int quiz2 = 0;// let’s give another quiz! …// indicate students that took quiz 2 // determine if student #4 took both final int s7 = 1<<7; if ( (quiz1&s7) != 0 && (quiz2&s7) != 0 ) { …//student 7 took both }

Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0;// indicate that no one took the quiz quiz1 |= 1 << 7;// indicate that student #7 took the quiz quiz1 |= 1 << 4;// indicate that student #4 took the quiz quiz1 &= ~(1 << 7);// Oops! Student #7 didn’t take the quiz. Turn it off. int quiz2 = 0;// let’s give another quiz! …// indicate students that took quiz 2 // How can we print out all students that took either quiz 1 or quiz 2 // (but not both)?

Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0;// indicate that no one took the quiz quiz1 |= 1 << 7;// indicate that student #7 took the quiz quiz1 |= 1 << 4;// indicate that student #4 took the quiz quiz1 &= ~(1 << 7);// Oops! Student #7 didn’t take the quiz. Turn it off. int quiz2 = 0;// let’s give another quiz! …// indicate students that took quiz 2 // determine whether a student took either quiz 1 or quiz 2 (but not both) int oneOrTheOther = quiz1 ^ quiz2; // How do we print the students out?

Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0;// indicate that no one took the quiz quiz1 |= 1 << 7;// indicate that student #7 took the quiz quiz1 |= 1 << 4;// indicate that student #4 took the quiz quiz1 &= ~(1 << 7);// Oops! Student #7 didn’t take the quiz. Turn it off. int quiz2 = 0;// let’s give another quiz! …// indicate students that took quiz 2 // determine whether a student took either quiz 1 or quiz 2 (but not both) int oneOrTheOther = quiz1 ^ quiz2; // print out the students if ( (oneOrTheOther & 1) != 0 )System.out.println( “one” ); oneOrTheOther >>>= oneOrTheOther; if ( (oneOrTheOther & 1) != 0 )System.out.println( “two” ); …

END RECALL

IA32 BITWISE LOGICAL INSTRUCTIONS

AND

Operation: DEST ← DEST AND SRC; Flags Affected: – The OF and CF flags are cleared. – the SF, ZF, and PF flags are set according to the result. – The state of the AF flag is undefined.

NEG

NEG (2’s) Operation: IF DEST = 0 THEN CF ← 0 ELSE CF ← 1; FI; DEST ← – (DEST) Flags Affected: – The CF flag set to 0 if the source operand is 0; otherwise it is set to 1. – The OF, SF, ZF, AF, and PF flags are set according to the result.

NOT

NOT (1’s) Operation: DEST ← NOT DEST; Flags Affected: – None.

OR

Operation: DEST ← DEST OR SRC; Flags Affected: – The OF and CF flags are cleared. – The SF, ZF, and PF flags are set according to the result. – The state of the AF flag is undefined.

XOR

Operation: DEST ← DEST XOR SRC; Flags Affected: – The OF and CF flags are cleared. – The SF, ZF, and PF flags are set according to the result. – The state of the AF flag is undefined.

BIT SHIFT INSTRUCTIONS: ROTATE

Rotate left instructions

Rotate right instructions

Rotate

Rotate instructions notes (Recall CL is the CL register (recall ECX, CX, CH, CL).) “The count operand is an unsigned integer that can be an immediate or a value in the CL register. The processor restricts the count to a number between 0 and 31 by masking all the bits in the count operand except the 5 least significant bits.” from IA-32 Intel Architecture Software Developer’s Manual Volume 2B: Instruction Set Reference, N-Z

Rotate instruction notes Flags Affected: – The CF flag contains the value of the bit shifted into it. – The OF flag is affected only for singlebit rotates (see reference); it is undefined for multi- bit rotates. – The SF, ZF, AF, and PF flags are not affected.

BIT SHIFT INSTRUCTIONS: SHIFTS

Shifts Notes: – SAL and SHL are exactly the same. – SAR preserves the sign. – SHR always shift in a zero to the sign bit so neg will become pos.

Shifts Notes: – SAL and SHL are exactly the same.

Shifts Notes: – SAR preserves the sign. – SHR always shift in a zero to the sign bit so neg will become pos.

SHL/SAL (same) example

SHR example

SAR example (preserving sign)

Another SAR example (preserving sign)

Shift instructions notes Flags Affected: – The CF flag contains the value of the last bit shifted out of the destination operand; it is undefined for SHL and SHR instructions where the count is greater than or equal to the size (in bits) of the destination operand. – The OF flag is affected only for 1-bit shifts (see reference); otherwise, it is undefined. – The SF, ZF, and PF flags are set according to the result. – If the count is 0, the flags are not affected. – For a non-zero count, the AF flag is undefined.

SOME INTERESTING ALGORITHMS

Some interesting algorithms We can zero something by xor’ing it with itself.

Some interesting algorithms How does one swap a and b using a temporary variable? One can perform memory-less swap using xor x = x ^ y; y = x ^ y; x = x ^ y; Note: A^B == B^A. So: x ^= y; y ^= x; x ^= y; Does Java actually have such an operator?

Some interesting algorithms How does one swap a and b using a temporary variable? One can perform memory-less swap using xor x = x ^ y; y = x ^ y; x = x ^ y; Note: A^B == B^A. So: x ^= y; y ^= x; x ^= y; Does Java actually have such an operator? Try:x = 1011 y = 0110

Operator precedence The closer to the top of the table an operator appears, the higher its precedence. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left. tutorial/java/nutsandbolts/operators.html tutorial/java/nutsandbolts/operators.html

Some interesting algorithms We can implement modulo 0xff (or 0xf, 0xff, 0xfff, 0xffff, …) counters via: inceax andeax, 0FFh

Some interesting algorithms We can multiply by powers of 2 by shifting to the left. More generally, some multiplies can be replaced by shifts and adds. (These may be more efficient.) i*2 == i<<1 i*3 == (i<<1) + i; i*10 == (i<<3) + (i<<1) We can divide by powers of 2 by shifting to the right.

Timing operations in Java //use the following below to compare the following (do not worry about overflow): //i+i and the equivalent in terms of multiplication //i+i and the equivalent in terms of bit shift //i/2 and the equivalent in terms of bit shift //i*i*i and the equivalent in terms of Math.pow() class MyTime { public static void main ( String[] args ) { long start = System.currentTimeMillis(); for (int i=0; i< ; i++) { //repeat 2 billion times //insert operation(s) to be repeated here } long stop = System.currentTimeMillis(); System.out.println( "elapsed time=" + (stop-start)/ "s" ); } See also nanoTime().

MULTIPLICATION SANS MULTIPLY (SANS!? THAT FANCY!)

Multiplication sans multiply What if we don’t (our processor doesn’t) have a multiply instruction? – (Or, how might multiply be implemented in microcode?)

Recall: Multiplication (decimal)

Recall: Multiplication (binary)

It’s interesting to note that binary multiplication is a sequence of shifts and adds of the first term (depending on the bits in the second term is missing here because the corresponding bit in the second terms is 0.

Recall: Multiplication (binary) Algorithm for r = a x b: r = 0 t = a loop over n bits: shift b one bit k to right if k was set then r += t shift t one bit to the left go to loop is missing here because the corresponding bit in the second terms is 0.

Advanced instructions BSWAP - byte swap BT - bit test XCHG - exchange register/memory with register – Atomic! BTS - bit test and set – Not atomic! LOCK – makes the next instruction atomic