user3467433
user3467433

Reputation: 385

Representing Float into Binary

So I recently learned the way to represent a float into a binary string, and I came across a really annoying confusion.

Lets say I have the float 10.25. In binary this would be 1010.01

Taking the exponents, this would be 1.01001 x 2^(3). So the sign bit is 0, the exponent is the unsigned 8bit binary of 127 + 3, which would become 10000010. Now for the fraction part, this should be 00000000 00000000 0001001 (23 bits)

Putting them all together, 0 10000010 00000000 00000000 0001001.

But when I put this into a conversion website, it gives me this: enter image description here It seems that the Mantissa part has been flipped by each 8 bits, maybe because of the little endian implementation. But here is the thing.

From the Big Endian Mantissa 00000000 00000000 0001001,

shouldn't the Little Endian Mantissa be 10010000 00000000 0000000?

The image says that the binary string is 0 10000010 0100100 00000000 00000000

Upvotes: 0

Views: 926

Answers (1)

mw215
mw215

Reputation: 323

As you correctly wrote

  • the sign is 0
  • the exponent is 10000010
  • the mantissa is 01001000000000000000000 (added a few trailing 0's in order to fill the 32-bit pattern)

Putting these parts together we get

0 10000010 01001000000000000000000

Now, arranging the bits into bytes, we have

01000001 00100100 00000000 00000000

This is exactly the representation of 10.25 according to the IEEE 754 single precision format. In big endian machines the 4 bytes or ordered as above while in little endian machines they are ordered the other way round. That is

  • 10.25 in big endian machines is 01000001 00100100 00000000 00000000
  • 10.25 in little endian machines is 00000000 00000000 00100100 01000001

The IEEE 754 standard specifies that

  • the first bit represents the sign
  • the next 8 bits represents the exponent
  • the remaining bits represents the mantissa minus 1

IEEE 754

If m1..m23 are the bits making up the mantissa, then m1 weighs 2^-1 and m23 weighs 2^-23.
Simply, it's the standard that dictates the meaning of each bit.

Upvotes: 2

Related Questions