87
Bits and Bytes

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

Embed Size (px)

Citation preview

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

Bits and Bytes

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

BITWISE OPERATORS

Page 3: 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: !

Show truth tables.

Page 4: 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?

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

RECALL BITWISE (INTEGER) OPERATORS

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

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

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

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.

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

Bitwise & (and)

--------

0x49 &

0x3a

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

Bitwise & (and)

--------

0x49 &

0x3a

-----------

1001 0100 &

1010 0011

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

Bitwise & (and)

--------

0x49 &

0x3a

1000 0000

-----------

1001 0100 &

1010 0011

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

Bitwise & (and)

0x08

--------

0x49 &

0x3a

1000 0000

-----------

1001 0100 &

1010 0011

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

Bitwise & (and)

--------

0xf0f0 &

0xff00

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

Bitwise & (and)

0xf000

--------

0xf0f0 &

0xff00

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

Bitwise | (or)

--------

0x49 |

0x3a

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

Bitwise | (or)

0x7b

--------

0x49 |

0x3a

1011 0111

-----------

1001 0100 |

1010 0011

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

Bitwise ^ (xor)

--------

0x49 ^

0x3a

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

Bitwise ^ (xor)

0x73

--------

0x49 ^

0x3a

0011 0111

-----------

1001 0100 ^

1010 0011

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

~ (1’s complement)

• Given 0x3a, what is ~0x3a?

• Given 0x0022, what is ~0x0022?

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

- (2’s complement)

• Given 0x3a, what is -0x3a?

• Given 0x0022, what is -0x0022?

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

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 shiftedB = shift distance

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

Shift operators

Left shift: A << B

BA 2*

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

Shift operators

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

B

A

2

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

Shift operators

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

otherwise~2

0A if

BBA

BABA

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

Examples

10 << 1

7 << 3

-1 << 2

BA 2*

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

Examples

10 << 1 20

7 << 3 56

-1 << 2 -4

BA 2*

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

Examples

10 >> 1

27 >> 3

-50 >> 2

B

A

2

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

Examples

10 >> 1 5 0000 1010 >> 1 = 0000 0101

27 >> 3 3 0001 1011 >> 3 = 0000 0011

-50 >> 2 -13 1100 1110 >> 2 = 1111 0011What is 1111 0011?0000 1100 + 1 = 0000 1101 = 13So 1111 0011 = -13

Preserves sign.

B

A

2

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

Examples

-50 >>> 2

0xff >>> 4

otherwise~2

0A if

BBA

BABA

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

Examples

-50 >>> 2 51 1100 1110 >>> 2 = 0011 0011

0xff >>> 4 15 1111 1111 >>> 4 = 0000 1111

Does not preserve sign.

otherwise~2

0A if

BBA

BABA

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

Examples

byte b = ~12;

flags = flags & ~0xf;

10 & 7

if ((flags & 0xf) != 0)

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

Examples

10 | 7

flags = flags | 0xf;

flags |= 0xa;

10 ^ 7

~0x97

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

Examples

int bits = 1;

bits = bits << 1;

bits = bits << 2;

bits = bits >> 3;

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

Examples

int b1 = 0x65;int b2 = 0xaf;

int x = b1 & b2;

int y = b1 ^ b2;

int z = b1 | b2;

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

Problem 1

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

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

Problem 2

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

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

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?

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

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?

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

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.

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

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.

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

Problem 3Say 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 quizquiz1 |= 1 << 7; // indicate that student #7 took the quizquiz1 |= 1 << 4; // indicate that student #4 took the quizquiz1 &= ~(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?

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

Problem 3Say 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 quizquiz1 |= 1 << 7; // indicate that student #7 took the quizquiz1 |= 1 << 4; // indicate that student #4 took the quizquiz1 &= ~(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 bothfinal int s7 = 1<<7;if ( (quiz1&s7) != 0 && (quiz2&s7) != 0 ) {

… //student 7 took both}

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

Problem 3Say 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 quizquiz1 |= 1 << 7; // indicate that student #7 took the quizquiz1 |= 1 << 4; // indicate that student #4 took the quizquiz1 &= ~(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)?

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

Problem 3Say 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 quizquiz1 |= 1 << 7; // indicate that student #7 took the quizquiz1 |= 1 << 4; // indicate that student #4 took the quizquiz1 &= ~(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?

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

Problem 3Say 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 quizquiz1 |= 1 << 7; // indicate that student #7 took the quizquiz1 |= 1 << 4; // indicate that student #4 took the quizquiz1 &= ~(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 studentsif ( (oneOrTheOther & 1) != 0 ) System.out.println( “one” );oneOrTheOther >>>= oneOrTheOther;if ( (oneOrTheOther & 1) != 0 ) System.out.println( “two” );…

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

END RECALL

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

IA32 BITWISE LOGICAL INSTRUCTIONS

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

AND

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

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.

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

NEG

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

NEG (2’s)

• Operation:IF DEST = 0THEN CF ← 0ELSE 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.

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

NOT

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

NOT (1’s)

• Operation:DEST ← NOT DEST;

• Flags Affected:–None.

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

OR

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

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.

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

XOR

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

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.

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

BIT SHIFT INSTRUCTIONS: ROTATE

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

Rotate left instructions

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

Rotate right instructions

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

Rotate

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

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

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

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.

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

BIT SHIFT INSTRUCTIONS: SHIFTS

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

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.

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

Shifts

Notes:– SAL and

SHL are exactly the same.

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

Shifts

Notes:– SAR preserves

the sign.

– SHR always shift in a zero to the sign bit so neg will become pos.

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

SHL/SAL (same) example

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

SHL/SAL (same) example

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

SHR example

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

SAR example (preserving sign)

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

Another SAR example(preserving sign)

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

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.

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

SOME INTERESTING ALGORITHMS

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

Some interesting algorithms

• We can zero something by xor’ing it with itself.

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

Some interesting algorithms

• How does one swap a and b using a temporary variable?

• One can perform memory-less swap using xorx = 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?

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

Some interesting algorithms

• How does one swap a and b using a temporary variable?

• One can perform memory-less swap using xorx = 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

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

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

• http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

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

Some interesting algorithms

• We can implement modulo 0xff (or 0xf, 0xff, 0xfff, 0xffff, …) counters via:inc eaxand eax, 0FFh

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

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<<1i*3 == (i<<1) + i;i*10 == (i<<3) + (i<<1)

• We can divide by powers of 2 by shifting to the right.

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

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<2000000000; i++) { //repeat 2 billion times

//insert operation(s) to be repeated here

} long stop = System.currentTimeMillis(); System.out.println( "elapsed time=" + (stop-start)/1000.0 + "s" ); }}

See also nanoTime().

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

MULTIPLICATION SANS MULTIPLY(SANS!? THAT FANCY!)

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

Multiplication sans multiply

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

microcode?)

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

Recall: Multiplication (decimal)

143

130

13

11

13

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

Recall: Multiplication (binary)

10001111

1101000

11010

1101

1011

1101

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

Recall: Multiplication (binary)

10001111

1101000

11010

1101

1011

1101

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.

110100 is missing here because the corresponding bit in the second terms is 0.

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

Recall: Multiplication (binary)

10001111

1101000

11010

1101

1011

1101

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 110100 is missing here

because the corresponding bit in the second terms is 0.

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

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