Input: "12" ASCII representation is: 00110001 00110010 (1) Map each ASCII code to it's 2's complement equivalent: 00000001 00000010 (2) Compute the value (in binary) using highschool formula: 00000001×000010101 + 00000010×000010100 = 00001100 |
|
The negative sign ("dash" character '-') is represented by the ASCII code 00101101.
If the first character of the (input) string is '-':
|
Input: "-12" ASCII representation is: 00101101 00110001 00110010 (0) If the first character is '-' then { Remember the negative sign and discard it Remaining input: 00110001 00110010 } (1) Map each ASCII code to it's 2's complement equivalent: 00000001 00000010 (2) Compute the value (in binary) using highschool formula: 00000001000010101 + 00000010000010100 = 00001100 (3) If number was negative { negate result (flip all bits and add 1) = 11110011 + 1 = 11110100 } |
The algorithm expressed in Java is as follows:
/* ----------------------------------------------------- Power(k): Return 10^k in Binary 2's complement code ------------------------------------------------------ */ public static int Power(int k) { int r = 1; // Stores: 00000000000000000000000000000001 in variable r for (int i = 0; i < k; i++ ) { r = r * 10; // The Java compiler will convert 10 into the // binary number 00000000000000000000000000001010 // This statement will multiply bin number r // with 00000000000000000000000000001010 } return r; // Returns a BINARY number in 2's compl // that represents the VALUE 10^k } /* -------------------------------------------------------------- parseInt(s): Ascii to Integer conversion function Input: String (ASCII) representation of a number Output: int (2's complement) representation of same number -------------------------------------------------------------- */ public static int myParseInt(String s) { int[] digit = new int[20]; // Used to store individual digits in String s int value, sign; int i, pos; int len; /* ------------------------- Check for negative sign ------------------------- */ if (s.charAt(0) == '-') { sign = -1; pos = 1; // Start processing at charAt(1) } else { sign = 0; pos = 0; // Start processing at charAt(0) } /* ------------------------------------------- Convert each character digit into 2s compl (and keep a count on the number of digits ------------------------------------------- */ len = 0; // len counts # digits (without the leading '-') for ( i = pos; i < s.length(); i++ ) // Convert ASCII digits { digit[len] = s.charAt(i) - 48; len++; // Count # digits in number } /* ------------------------------------------ Compute the absolute value of the number ------------------------------------------ */ value = 0; for (int k = 0; k < len; k++) { /* --------------------------------------------------------------- Value of the digits: digit[0] ... digit[k] ... digit[len-2] digit[len-1] 10^((len-1)-k) 10^1 10^0 ---------------------------------------------------------------- */ pos = (len - 1) - k; value = value + digit[k]*Power( pos ); // High school knowledge.... } /* ======================================================== Negate 2's complement representation if first character of input was '-' ========================================================= */ if (sign == -1) value = -value; // Compute the negative value (= flip bits and add 1 !!) return(value); // Return a BINARY 2's compl code } |
The algorithm is doing exactly what I describe with the examples:
(1) In the "Check for negative sign and process input" code segment we check if the first character in the input string s is a dash ('-') If so: we start at the 2nd character in the input (skipping the '-' char) If not: we start at the 1st charcater in the input We take each character in the input string and map the character to it 2's complement equivalent using: s.charAt(i) - 48 After this processing, the array "digit[ ]" will contains the digits of the number in 2's complement representation (2) In the "Compute the absolute value of the number" code segment we use the highschool formula to compute the value of the decimal number (in binary) The function Power(pos) -- where pos is the position of the digit, returns the value 10pos in binary. (3) Finally, in the Compute the absolute value of the number..." code segment we apply the 2's complement negation operation on the result if the first character was a dash (-') |
How to run the program:
|
I will just show it for those who are interested:
public static int myParseInt(String s) { int value, sign; int pos; /* ------- Check for negative sign ------- */ if (s.charAt(0) == '-') { sign = -1; pos = 1; // Start at digit s.charAt(1) } else { sign = 1; pos = 0; // Start at digit s.charAt(0) } /* ------------------------------------------ Compute the absolute value of the number ------------------------------------------ */ value = 0; for (int k = pos; k < s.length(); k++) { value = 10*value + ( s.charAt(k) - 48 ); // High school knowledge.... } /* ======================================================== Return the 2s complement representation ========================================================= */ return (sign*value); // If sign == -1, we will return // the 2s complement negative value ! } |
How to run the program:
|