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