Goal :
For the second programming assignment, you have to implement a system that provides the
conversion between signed decimal and 16-bit binary number. We use 2's complement representation for the binary.
Click here to see what 2's complement representation
means.
The 16-bit binary is represented by a string of 0's and 1's whose length is less than or equal to 16.
You can download Hw2.class and run it to get some idea for the screen design.
Detail with be discussed in class.
The algorithms for BTD and DTB are given as follows :
Be careful, the following are in pseudo code, not in 100 percent valid Java syntax !
Convert signed integer n to k-bit 2's complement binary
static String DTB(int n ) { if n is negative then n = n + pow(2,k) String binary = "" ; while ( n > 0 ) { if ( n % 2 == 0 ) then binary = "0" + binary else binary = "1" + binary n /= 2 ; } return bianry }
Convert from 2's complement binary to decimal :
static int BTD(String binary) { int res = 0 ; for ( int i = 0 ; i < binary.length() ; i++ ) if binary.charAt(i) == '1' then res = 2*res + 1 else res = 2*res if binary is negative then res = res - pow(2,k) ; return res }