Docsity

Data Structures - Data Structures and Advanced Programming - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Data Structures - Data Structures and Advanced Programming - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

CS61B Lecture #15: Integers

Today:

  • Integer Types

Readings for Today: Assorted Materials on Java, Chapter 3.

Readings for Upcoming Topics: Data Structures (Into Java), Chap- ter 1; Head First Java, Chapter 16.

Reminder: You’re testing Project #1 now, right?

Integer Types and Literals

Type Bits Signed? Literals byte 8 Yes short 16 Yes

char 16 No

’a’ // (char) 97 ’\n’ // newline ((char) 10) ’\t’ // tab ((char) 8) ’\’ // backslash ’A’, ’\101’, ’\u0041’ // == (char) 65

int 32 Yes

0100 // Octal for 64 0x3f, 0xffffffff // Hexadecimal 63, -1 (!)

long 64 Yes

123L, 01000L, 0x3fL 1234567891011L

  • “N bits” means that there are 2 N^ integers in the domain of the type.
  • If signed, range of values is − 2 N^ −^1 .. 2 N^ −^1 − 1.
  • If unsigned, only non-negative numbers, and range is 0 .. 2 N^ − 1.
  • Negative numerals are just negated (positive) literals.
  • Use casting for byte and short: (byte) 12, (short) 2000.

Modular Arithmetic II

  • (byte) (64*8) yields 0, since 512 − 0 = 2 · 28.
  • (byte) (64*2) and (byte) (127+1) yield -128, since 128 − (−128) = 1 · 28.
  • (byte) (345*6) yields 22, since 2070 − 22 = 8 · 28.
  • (byte) (-30*13) yields 122, since − 390 − 122 = − 2 · 28.
  • (char) (-1) yields 216 − 1 , since − 1 − (2^16 − 1) = 2^16.
  • Natural definition for a machine that uses binary arithmetic:

Type char Type byte

  • Terminology: rightmost (units) bit is bit 0, 2s bit is bit 1.
  • Hence, changing bit n modifies value by 2 n; truncating on left to n bits computes modulo 2 n.

Negative numbers

  • Why this representation for -1?

(^1 )

  • − (^1 ) = 0 1 | (^000000002) Only 8 bits in a byte, so bit 8 falls off, leaving 0.
  • The truncated bit is in the 28 place, so throwing it away gives an equal number modulo 28. All bits to the left of it are also divisible by 28.
  • On unsigned types (char), arithmetic is the same, but we choose to represent only non-negative numbers modulo 216 : (^1 )
  • 2^16 − (^1 ) = 2^16 + 0 1 | (^00000000000000002)

Promotion

  • Arithmetic operations (+, *,... ) promote operands as needed.
  • Promotion is just implicit conversion.
  • For integer operations,
    • if any operand is long, promote both to long.
    • otherwise promote both to int.
  • So,

aByte + 3 == (int) aByte + 3 // Type int aLong + 3 == aLong + (long) 3 // Type long ’A’ + 2 == (int) ’A’ + 2 // Type int aByte = aByte + 1 // ILLEGAL (why?)

  • But fortunately,

aByte += 1; // Defined as aByte = (byte) (aByte+1)

  • Common example:

// Assume aChar is an upper-case letter char lowerCaseChar = (char) (’a’ + aChar - ’A’); // why cast?

Bit twiddling

  • Java (and C, C++) allow for handling integer types as sequences of bits. No “conversion to bits” needed: they already are.
  • Operations and their uses:

Mask Set Flip Flip all 00101100 00101100 00101100 & 10100111 | 10100111 ^ 10100111 ~ 10100111 00100100 10101111 10001011 01011000

  • Shifting:

Left Arithmetic Right Logical Right 10101101 << 3 10101101 >> 3 10101100 >>> 3 01101000 11110101 00010101

  • What is:

x << n? x >> n? (x >>> 3) & ((1<<5)-1)?.

Bit twiddling

  • Java (and C, C++) allow for handling integer types as sequences of bits. No “conversion to bits” needed: they already are.
  • Operations and their uses:

Mask Set Flip Flip all 00101100 00101100 00101100 & 10100111 | 10100111 ^ 10100111 ~ 10100111 00100100 10101111 10001011 01011000

  • Shifting:

Left Arithmetic Right Logical Right 10101101 << 3 10101101 >> 3 10101100 >>> 3 01101000 11110101 00010101

  • What is:

x << n? = x · 2 n. x >> n? (x >>> 3) & ((1<<5)-1)?.

Bit twiddling

  • Java (and C, C++) allow for handling integer types as sequences of bits. No “conversion to bits” needed: they already are.
  • Operations and their uses:

Mask Set Flip Flip all 00101100 00101100 00101100 & 10100111 | 10100111 ^ 10100111 ~ 10100111 00100100 10101111 10001011 01011000

  • Shifting:

Left Arithmetic Right Logical Right 10101101 << 3 10101101 >> 3 10101100 >>> 3 01101000 11110101 00010101

  • What is:

x << n? = x · 2 n. x >> n? = bx/ 2 nc (i.e., rounded down). (x >>> 3) & ((1<<5)-1)?.

Bit twiddling

  • Java (and C, C++) allow for handling integer types as sequences of bits. No “conversion to bits” needed: they already are.
  • Operations and their uses:

Mask Set Flip Flip all 00101100 00101100 00101100 & 10100111 | 10100111 ^ 10100111 ~ 10100111 00100100 10101111 10001011 01011000

  • Shifting:

Left Arithmetic Right Logical Right 10101101 << 3 10101101 >> 3 10101100 >>> 3 01101000 11110101 00010101

  • What is:

x << n? = x · 2 n. x >> n? = bx/ 2 nc (i.e., rounded down). (x >>> 3) & ((1<<5)-1)? 5-bit integer, bits 3–7 of x.