Wakka Wakka Wakka
Wakka Wakka Wakka

Reputation: 281

Outputting a value as a 4-byte unsigned integer in little endian form

I'm trying to output a value as a 4-byte unsigned integer (in little endian form) and am getting confused about something.

If I want to output the number 1 in this form does it appear as:

00000001 00000000 00000000 00000000

OR

10000000 00000000 00000000 00000000

Similarly, if I wanted to output the number 256 would it take the form of:

11111111 00000001 00000000 00000000

I just need clarification. I've read some conflicting things from various places on the internet.

Upvotes: 1

Views: 365

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

Endianness effects the order of bytes in multibyte words, not the order of bits in a byte itself. So 1 is

00000001 00000000 00000000 00000000

and 256 is

00000000 00000001 00000000 00000000

Upvotes: 1

Antimony
Antimony

Reputation: 39461

Memory is usually represented as a sequence of discrete bytes, so it doesn't make sense to talk about which bits are adjacent to each other. It's just a matter of display formatting. That said, it's traditional to display the LSB on the right, so in your example it would be

00000001 00000000 00000000 00000000

In hex, which is what you normally use when examining binaries, you'd get

01 00 00 00

Also, in your second example, you accidently represented 511, not 256. The correct bit representation for 256 is

00000000 00000001 00000000 00000000

Upvotes: 1

Related Questions