how to convert a hex or binary number to decimal and vice versa using a any java mehtod, can any one help.
and one more thing that can any one explain how computer store and retrieve - sign . i am so much confusd with left and right shifts
i have mentioned 2 codes here.
1). our input variable "int num = 0xFFFFFFE". how after all operations the result
comes in decimal
.
2). we have a String array of hex numbers. how the resultant values are searched in the array.and why weare doing
"[(b >> 4) & 0x0f]" and [b & 0x0f].
any solid reasons
examle1
// Left shifting as a quick way to multiply by 2.
class MultByTwo {
public static void main(String args[]) {
int i;
int num = 0xFFFFFFE;
for(i=0; i<4; i++) {
num = num << 1;
System.out.println(num);
}
}
}
The program generates the following output:
536870908
1073741816
2147483632
-3
example 3
// Masking sign extension.
class HexByte {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}
Here is the output of this program:
b = 0xf1