Sam Moran

Ranch Hand

Posts: 86

posted 14 years ago

I can convert hex to bianry but I lose the leading zeros. I need the leading zeros to assist me in my calculations. I have a hex value of:

017B7020 coming in and binary out of:

1011110110111000000100000

I've lost the 4 zeros for the frist zero and in addition I have lost the 3 leading zeros for the one.

I have tried a mask and that has not helped. I need the full 32 bits from that and other hex values.

Is this possible? Thank you.

017B7020 coming in and binary out of:

1011110110111000000100000

I've lost the 4 zeros for the frist zero and in addition I have lost the 3 leading zeros for the one.

I have tried a mask and that has not helped. I need the full 32 bits from that and other hex values.

Is this possible? Thank you.

We make a living by what we get, we make a life by what we give!

Neil Laurance

Ranch Hand

Posts: 183

Sam Moran

Ranch Hand

Posts: 86

Neil Laurance

Ranch Hand

Posts: 183

posted 14 years ago

0x80000000 is a bit mask, with a single '1' in the most significant bit of the 4 bytes used to represent an integer, that is:

0x80000000 = 10000000 00000000 00000000 00000000 (base2)

The while loop performs a logical AND of the number and the bit mask. A non zero return value indicates there was a bit at this position in the integer number, so append a '1' to the StringBuffer. Otherwise append a '0'. Then, before repeating the loop, we shift the bit 1 place to the right, filling in on the left with zeros (the >>> shift operator). That is:

10000000 00000000 00000000 00000000 >>> 1 =

01000000 00000000 00000000 00000000

etc..

Keep repeating the loop, until the '1' bit is completely shifted off to the right, and the mask then has the value 0.

Hope this helps :roll: :roll:

[ February 14, 2003: Message edited by: Neil Laurance ]

0x80000000 = 10000000 00000000 00000000 00000000 (base2)

The while loop performs a logical AND of the number and the bit mask. A non zero return value indicates there was a bit at this position in the integer number, so append a '1' to the StringBuffer. Otherwise append a '0'. Then, before repeating the loop, we shift the bit 1 place to the right, filling in on the left with zeros (the >>> shift operator). That is:

10000000 00000000 00000000 00000000 >>> 1 =

01000000 00000000 00000000 00000000

etc..

Keep repeating the loop, until the '1' bit is completely shifted off to the right, and the mask then has the value 0.

Hope this helps :roll: :roll:

[ February 14, 2003: Message edited by: Neil Laurance ]

Layne Lund

Ranch Hand

Posts: 3061

posted 14 years ago

Why are you doing calculations with a String object anyway? It seems to me it would be easier to simply convert the hex string to an int and do the calculations with the built-in operators. Usually hex and binary representations are only necessary for input and output. Internally, you can just use int or long to do all the calculations and bit manipulations.

Keep coding!

Layne

Keep coding!

Layne

Sam Moran

Ranch Hand

Posts: 86

posted 14 years ago

Neil, thank you for the explanation. I don't use hex or binary every day.

Layne, I am working on an assignment that will be a MIPS disassembler. I need the leading zeros to calculate the first bits and determine the OP code. Crazy RIGHT?

Layne, I am working on an assignment that will be a MIPS disassembler. I need the leading zeros to calculate the first bits and determine the OP code. Crazy RIGHT?

We make a living by what we get, we make a life by what we give!