Every number N can be represented as a sum of products of the form:
N = Bn * Bn + ... + B0 * B0
where B is the base and Bi < B
The base-B numeral representing N is the sequence [N]B = Bn...B0
In Computer Science the most common choices for B are:
B = 2 (binary representatio)
B = 8 (octal representation)
B = 10 (decimal representation)
B = 16 (hexadecimal notation
[42]10 = 42
[42]8 = 52 = 052
[42]16 = 2a = 0x2a
[42]2 = 101010
[101010]2 =
1 * 25 + 0 * 24 + 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20 =
0 + 2 * (1 + 2 * (0 + 2 * (1 + 2 * (0 + 2 * 1)))) = 42
[N]10 = 0;
while ([N]2 has more bits) {
[N]10 = 2 * [N]10;
[N]10 = [N]10 +
next bit
}
Let's convert
[N]2 = 101010
to base 10.
Starting from the left, multiply the first digit by 2:
[N]10 = 1 * 2 = 2
Add the next digit
[N]10 = 2 + 0 = 2
Multiply by 2
[N]10 = 2 * 2 = 4
Add the next digit:
[N]10 = 4 + 1 = 5
Multiply by 2:
[N]10 = 2 * 5 = 10
Add the next digit:
[N]10 = = 10 + 0 = 10
Multiply by 2:
[N]10 = 10 * 2 = 20
Add the next digit:
[N]10 = 20 + 1 = 21
Multiply by 2:
[N]10 = 2 * 21 = 42
Add the last digit:
[N]10 = 42 + 0 = 42
To go the other way, binary to decimal, repeatedly divide by 2. Record the remainders to get the bits in reverse order.
while (N > 0) {
bit[i++] = N % 2;
N = N / 2;
}
7/2 = 3 r 1
3/2 = 1 r 1
1/2 = 0 r 1
8/2 = 4 r 0
4/2 = 2 r 0
2/2 = 1 r 0
1/2 = 0 r 1
42/2 = 21 r 0
21/2 = 10 r 1
10/2 = 5 r 0
5/2 = 2 r 1
2/2 = 1 r 0
1/2 = 0 r 1
There are many calculators on the web that will convert between base 2 and base 10. For example:
http://mistupid.com/computers/binaryconv.htm
The best way to do this is to memorize the binary representations of each hex digit:
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
a = 1010
b = 1011
c = 1100
d = 1101
e = 1110
f = 1111
Example:
101010 = 0010|1010 = 2a
Finish this definition, then write a test harness for it:
class BaseConverter {
// bDigits = a string of binary digits
(0, 1)
public static int b2d(String bDigits) {
int result = 0;
// implement the binary to decimal
algorithm here
return result;
}
// oDigits = a string of octal digits
(0 – 7)
public static int o2d(String hDigits) {
int result = 0;
// implement a modification of the
b2d algorithm here
return result;
}
// hDigits = a string of hex digits (0
– 9, a – f)
public static int h2d(String hDigits) {
int result = 0;
// implement a modification of the
b2d algorithm here
return result;
}
public static String d2b(int n) {
String bits = "";
// implement the decimal to binary
algorithm here
return result;
}
// more later
}
public class TestBaseConverter {
public static void main(String[] args)
{
// test code goes here
}
}
We can add binary numbers the same way we add decimal numbers. We just have to remember that 1 + 1 = 0 carry 1:
101010 (42)
+ 111
(+7)
------
110001 (49)
The algorithms for multiplication, subtraction, and division work, too.
In the Java virtual machine there are four integer types:
byte, short, int, long
A byte is a signed 8-bit integer ranging from -27 (= -128) up to 27-1 (= 127)
A short is a signed 16-bit integer ranging from -215 (= -32,768) up to 215 – 1 (= 32,767)
An int is a signed 32-bit integer ranging from -231 (= -2,147,483,648) up to 231 – 1 (= 2,147,483,647 )
An long is a signed 64-bit integer ranging from -263 (=-9,223,372,036,854,775,808) up to 263 – 1 (= 9,223,372,036,854,775,807 )
The sign-magnitude scheme for representing N-bit signed integers is to simply reserve the left-most bit for the sign, 0 for positive, 1 for negative.
The problem with this scheme is that there are two representations of 0:
-0 = 1000
+0 = 0000
In the Ones-complement System, the negative of a number is its ones-complement:
-x = ~x
The ones complement is the result of inverting each bit. For example:
0101 = 5
1010 = -5
Note that the left-most bit indicates the sign.
The problem with this system is that we have two representations for 0:
0000 = 0
1111 = -0 = 0
Most machines prefer the twos complement system.
In the N-bit Twos complement system the negative of x is 2N – x % 2N
For example, for N = 4:
5 = 0101
-5 = 16 – 5 % 16 = 11 % 16 = 11 = 1011
-(-5) = 16 – (-5) % 16 = 16 + 5 % 16 = 21 % 16 = 5 = 0101
Note that –x = ~x + 1
The problem with this system is that the smallest number has no negative. For example:
- (-8) = 16 – (-8) % 16 = 24 % 16 = 8 = 1111
N SM 1C 2C
-8 ???? ???? 1000
-7 1111 1000 1001
-6 1110 1001 1010
-5 1101 1010 1011
-4 1100 1011 1110
-3 1011 1100 1101
-2 1010 1101 1110
-1 1001 1110 1111
0 0000/1000 0000/1111 0000
1 0001 0001 0001
2 0010 0010 0010
3 0011 0011 0011
4 0100 0100 0100
5 0101 0101 0101
6 0110 0110 0110
7 0111 0111 0111