Question

This isn't a question as much as it's a sanity check!

If you needed to read 4 bytes into Java as a bitmask in Big endian and those bytes were:

0x00, 0x01, 0xB6, 0x02.

Making that into an int would be: 112130

The binary would be: 00000000000000011010011000000010

The endian of a series of bytes wouldn't affect the bit position, would it?

Thanks

Tony

Was it helpful?

Solution

Endian-ness reflects the ordering of bytes, but not the ordering of the bits within those bytes.

Let's say I want to represent the (two-byte) word 0x9001. If I just type this out in binary, that would be 1001000000000001.

If I dump the bytes (from lower address to higher) on a big-endian machine, I would see 10010000 00000001.

If I dump the bytes (from lower address to higher) on a little-endian machine, I would see 00000001 10010000.

OTHER TIPS

In general, if the thing you're reading from is giving you whole bytes, then you don't need to worry about the order of bits making up those bytes: it is just the order of the bytes that matters, as you correctly suppose.

The time you might have to worry about the "endianness" of individual bits is where you're actually reading/writing a stream of bits rather than whole bytes (e.g. if you were writing a compression algorithm that operated at the bit level, you'd have to make a decision about what order to write the bits in).

The only thing you have to pay attention is how exactly you "read 4 bytes into Java" - that's where endianness matters and you can mess it up (DataInputStream assumes big endian). Once the value you've read has become the int 112130, you're set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top