Aj Prieto
Ranch Hand
Posts: 75
I'm trying to read in 4 bytes, then using a shift operator, merge it into a single int.

I'm wrote this code:

I'm not sure how I'm supposed to use shift operators, so I used the count variable. Can I do it that way or should I learn to use shift operators instead?

Henry Wong
author
Marshal
Posts: 21185
80
Aj Prieto wrote:I'm trying to read in 4 bytes, then using a shift operator, merge it into a single int.

First, what do you mean by "merge it into a single int"?? Are these four bytes, the four bytes that make up the bytes of the integer? Meaning if the four bytes are 0x02, 0x34, 0xAB, and 0x21, then the int is 0x21AB3402 (assuming little endian). If so, then yes, shifting is a good way to do it.

Henry

Aj Prieto
Ranch Hand
Posts: 75
Yes, the 4 bytes make up the integer. What do you mean by little endian?

I've tried using bit operators, though I'm not entirely sure if I'm using it correctly.

Am I using it the right way? Thanks

Henry Wong
author
Marshal
Posts: 21185
80
Aj Prieto wrote:Yes, the 4 bytes make up the integer. What do you mean by little endian?

Basically, "little endian" are people (from Liliput) who believe that hard-boiled eggs should be eaten from the little end first... In computer terms, little endian means the least significant byte comes first. So, the number 0x12345678, is stored in memory as 0x78 0x56 0x34 0x12 -- meaning the 0x78 byte is stored in the lowest memory address.

Aj Prieto wrote:
I've tried using bit operators, though I'm not entirely sure if I'm using it correctly.

Am I using it the right way? Thanks

Yes. It is correct. But as an FYI, you coded it for big endian -- so you like to eat the big end of the hard boiled egg first...

Henry

Aj Prieto
Ranch Hand
Posts: 75
Henry Wong wrote:
Basically, "little endian" are people (from Liliput) who believe that hard-boiled eggs should be eaten from the little end first... In computer terms, little endian means the least significant byte comes first. So, the number 0x12345678, is stored in memory as 0x78 0x56 0x34 0x12 -- meaning the 0x78 byte is stored in the lowest memory address.

That's interesting, sounds like a fun place to live. lol

Henry Wone wrote:
Yes. It is correct. But as an FYI, you coded it for big endian -- so you like to eat the big end of the hard boiled egg first...

Henry

Awww...but I dislike eggs . Is there an option to not eat the eggs? .

It is as simple as just switching the operator from << to >>?

edit: When I run a .huff file through it, I get a negative "count" value. Why is this happening, can I add 256 to it to get the correct value?

Thanks

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
For some background info, see Endianness on Wikipedia.

Campbell Ritchie
Sheriff
Posts: 49365
62

And yes, you do have to eat the eggs.

Henry Wong
author
Marshal
Posts: 21185
80
Aj Prieto wrote:
It is as simple as just switching the operator from << to >>?

No.... but it is as simple as doing the loop backwards (of course, that means that you need to use a regular loop -- not a foreach loop).

Aj Prieto wrote:
edit: When I run a .huff file through it, I get a negative "count" value. Why is this happening, can I add 256 to it to get the correct value?

Remember that integers are signed in Java, so for big endian, if the first byte is a negative number, then the result will be a negative number. As for correct value, that negative number is the correct value -- if you need a positive value, then you need to put it into something that can hold the four bytes as a positive number, such as a long type.

Henry

Winston Gutkowski
Bartender
Posts: 10422
63
Henry Wong wrote:Remember that integers are signed in Java, so for big endian, if the first byte is a negative number, then the result will be a negative number...

Providing you ALWAYS read in 4 bytes (or multiples of). If not, you'll probably need an extra shift at the end to maintain the correct endianness.

Winston