Bits and bytes Week 2: data representation Data is stored in the - - PDF document

bits and bytes week 2 data representation
SMART_READER_LITE
LIVE PREVIEW

Bits and bytes Week 2: data representation Data is stored in the - - PDF document

School of Computer Science, University of Birmingham. Java Lecture notes. M. D. Ryan. September 2000. Bits and bytes Week 2: data representation Data is stored in the computer as bits. (20 cr only) Bit stands for binary digit ; it has


slide-1
SLIDE 1

1

Week 2: data representation (20 cr only)

  • Data representation.

School of Computer Science, University of Birmingham. Java Lecture notes. M. D. Ryan. September 2000.

Bits and bytes

  • Data is stored in the computer as bits.

– Bit stands for binary digit; it has the value 0 or 1.

  • Bits by themselves are not much use, so we

define a byte to be 8 bits.

– A byte can store numbers from 0 to 255 (in binary, 00000000 to 11111111).

Interpretations of a byte

0000 0000 0000 0001 0000 0010 . . . 0111 1110 0111 1111 1000 0000 1000 0001 . . . 1111 1110 1111 1111 1 2 . . . 127

  • 128
  • 127

. . .

  • 2
  • 1

126 127 128 129 . . . 254 255 1 . . . 126 the byte signed unsigned

byte arithmetic

0 1 2 3 4 5 … 126 1 2 7

  • 1

2 8

  • 127
  • 126
  • 1
  • 2
  • 3
  • 4
  • 5

Bounding errors

  • When Java works with integer types

(byte, short, int, long) it uses a signed representation.

  • This means that, if positive numbers are

incremented beyond their maximum value, they become negative numbers.

Some numeric types

type values size range byte integer

  • 128 to 127

short integer

  • 32768 to 32767

int integer

  • 2147483648 to 2147483647

long integer

  • 9223372036854774808 to 9223372036854774808

float “real”

±3.4… e38 to ±1.4…e-45 9 decimal place precision

double “real”

±1.7…e308 to ±4.9…e-324 18 decimal place precision

f
slide-2
SLIDE 2

2

public class Bounds { public static void main (String[] args) { byte b=127; System.out.println(b+1); int i=2147483647; System.out.println(i+10); float f = (float)3.4e38; System.out.println(f*10); double d = 5e-324; System.out.println(d/10); } } prints -128 prints Infinity prints -2147483639 prints 0

Character interpretation of a byte

0000 0000 0000 0001 0000 0010 . . . 0010 0000 0010 1011 0011 0000 0011 0001 . . . 0100 0001 0100 0010 1 2 . . . 32 43 48 49 . . . 65 66 the byte character 90 . . . 97 98 . . . 122 123 127 <nul> ctrl-A ctrl-B . . . <space> + 1 . . . A B 0101 1010 0110 0001 0110 0010 0111 1010 0111 1011 0111 1111 Z a b z { <del>

Floating point numbers

  • You’ve probably noticed that scientific calculator

stores large and small numbers in two parts: the mantissa and the exponent of 10:

  • In Java, this is written 5.28573E23.

23

10 28573 . 5 ×

mantissa exponent

Floating point number representation

  • Internally, Java stores all floating numbers as a mantissa

and an exponent of 2.

  • For example, the number 0.2 is stored as
  • floats have 23 bits for the mantissa and 8 for the

exponent; doubles have 52 (mantissa) and 11 (exponent).

  • Note that (as in this example) numbers with finite

decimal representations may have infinite binary represenations.

3

2 ... 1001100110 . 1

×

Round-off errors

  • Round-off errors are a fact of life with floating

point numbers. E.g., try (1/3)*3 by hand, or by your calculator.

  • We also get effects like this in Java. Try

double f = 4.35; int n = (int)(100*f); System.out.println(n); // prints 434!

Why does it print 434?

  • Because, in binary,

100 * 4.35 = 1100100 * 100.010110011001100110… = 110110010.1111111111111…

  • and we truncate this by casting it to an int.
  • Moral of story: use Math.round:

double f = 4.35; int n = (int)Math.round(100*f);

slide-3
SLIDE 3

3

Comparing floating point numbers

  • It’s a bad idea to write

if (f1 == f2) ... because even if f1 and f2 are meant to be the same, they may evaluate to different numbers.

  • Therefore, write

if (Math.abs(f1-f2)<0.001) ... for a suitably chosen precision 0.001.

Compute an infinite series

  • +

+ + + + 16 1 8 1 4 1 2 1 1

  • We cannot compute it exactly, because

there are infinitely many terms

  • However, they make a negligible

contribution after a while

  • Keep adding terms until they are less than

10E-6.

public class Sum { public static void main (String[] args) { double sum = 0; double increment = 1.0; while (increment > 1E-6) { sum += increment; increment /= 2; System.out.println(sum); } } }