Data Input and Output
An Introduction with Examples in Java |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
boolean readBoolean()
byte readByte()
char readChar()
double readDouble()
float readFloat()
int readInt()
long readLong()
short readShort()
writeBoolean(boolean v)
writeByte(int v)
writeChar(int v)
writeDouble(double v)
writeFloat(float v)
writeInt(int v)
writeLong(long v)
writeShort(int v)
Using one byte to represent unsigned integers is straightforward. For example, using the usual conventions, the decimal number 26 can be represented as follows:
00011010
Using two bytes to represent unsigned integers is a little trickier. For example, which of the following is the correct representation of the number 256?
00000001 00000000
or
00000000 00000001
To "extract" the LSB:
01100001 00011010 & 00000000 11111111 _________________ 00000000 00011010
That is, perform a bitwise AND with the decimal number 255 (or 0x00FF).
To "extract" the MSB:
01100001 00011010 & 11111111 00000000 _________________ 01100001 00000000
That is, perform a bitwise AND with the decimal number 65280 (or 0xFF00).
Shifting Bits:
00000011 00000000 >> 8 _________________ 00000000 00000011
That is, 768 >> 8 evaluates to 3.
Combining Bytes:
00000011 00000000 | 00000000 00000001 _________________ 00000011 00000001
That is, 768 | 1 evaluates to 769.
What will be printed if we assign 97 + (65 << 8)
to i
?