CS 101
Computer Science 1 in Java
Prof. Alvarez
Example of the Binary Representation of Numbers
Here is an example of binary codes for integers and floating-point numbers
in a portion of a computer program. The language used for the program below
is not Java, but rather assembly language, which is much closer to the level
understood by the hardware inside the computer.
Example Program
The example program appears in the rightmost column below.
The memory of a computer like the one used for this example
is a sequence of consecutively numbered cells, each of which can store a
16-bit "word". A given program occupies a certain collection of words in
memory. The address of a statement in the program can be described by
giving the number of words from the start of the program, or offset,
of the first word of the statement.
The leftmost column below shows the address offsets of the corresponding
program statements in the rightmost column.
The second column from the left shows the hexadecimal encodings of the program
statements themselves.
0000 .data
0000 0003 piAsAnInt dw 3
0002 FFFD negPiAsAnInt dw -3
0004 4048F5C3 piToTwoDecimals dd 3.14
0000 .code
0000 main proc
0000 B8 ---- R mov ax, @data
0003 8E D8 mov ds, ax
0005 B4 09 mov ah, 9
0007 BA 0004 R mov dx, offset piToTwoDecimals
000A CD 21 int 21h
000C main endp
Numerical Representation in the Example Program
The above program defines three variables called piAsAnInt,
negPiAsAnInt, and piToTwoDecimals respectively. The decimal values
of these variables appear in the right column near the top of the file.
The binary encodings of these values appear in the second column from
the left. Thus, the decimal value -3 is represented by the hexadecimal
value FFFD. In binary, this is 1111111111111101, which is obtained from
the positive value 0000000000000011 by complementing each bit (ones
complement), and then adding 1 to obtain the twos complement.
The binary encoding of the floating point value 3.14 is a bit
more involved. It can be obtained by using the IEEE 754 standard
for floating point representation.