byte to int -> bit shifting confusion [modified]
-
Hi there, I got a little confused here, maybe someone can help with this noob question. Following code piece to convert byte to int (supposedly big endian):
unsigned int time = ((m_aHeader[4] & 0xff) << 24) |
((m_aHeader[5] & 0xff) << 16) |
((m_aHeader[6] & 0xff) << 8) |
(m_aHeader[7] & 0xff);Now why is this different than
unsigned int time = ((m_aHeader[4] ) << 24) |
((m_aHeader[5] ) << 16) |
((m_aHeader[6] ) << 8) |
(m_aHeader[7] );m_aHeader is a byte array. Edit: Just found, my problem only occurs when I use the first piece of code in java (in c++ it works fine). I create the bytes in c++ with
m_aHeader[4] = (BYTE) ((time >> 24) & 0xff);
m_aHeader[5] = (BYTE) ((time >> 16) & 0xff);
m_aHeader[6] = (BYTE) ((time >> 8) & 0xff);
m_aHeader[7] = (BYTE) (time & 0xff);If I read it in c++ with the further above mentioned code pieces, everything is fine. If I try this in java, though,
long time = ((m_aHeader[4] ) << 24) |
((m_aHeader[5] ) << 16) |
((m_aHeader[6] ) << 8) |
(m_aHeader[7] );modified on Wednesday, August 26, 2009 3:44 AM
-
Hi there, I got a little confused here, maybe someone can help with this noob question. Following code piece to convert byte to int (supposedly big endian):
unsigned int time = ((m_aHeader[4] & 0xff) << 24) |
((m_aHeader[5] & 0xff) << 16) |
((m_aHeader[6] & 0xff) << 8) |
(m_aHeader[7] & 0xff);Now why is this different than
unsigned int time = ((m_aHeader[4] ) << 24) |
((m_aHeader[5] ) << 16) |
((m_aHeader[6] ) << 8) |
(m_aHeader[7] );m_aHeader is a byte array. Edit: Just found, my problem only occurs when I use the first piece of code in java (in c++ it works fine). I create the bytes in c++ with
m_aHeader[4] = (BYTE) ((time >> 24) & 0xff);
m_aHeader[5] = (BYTE) ((time >> 16) & 0xff);
m_aHeader[6] = (BYTE) ((time >> 8) & 0xff);
m_aHeader[7] = (BYTE) (time & 0xff);If I read it in c++ with the further above mentioned code pieces, everything is fine. If I try this in java, though,
long time = ((m_aHeader[4] ) << 24) |
((m_aHeader[5] ) << 16) |
((m_aHeader[6] ) << 8) |
(m_aHeader[7] );modified on Wednesday, August 26, 2009 3:44 AM
Provided
m_aHeader[k]
(k=4..7) is abyte
, there is no difference, i.e. you may write ( note there is NO0xff
close tom_aHeader[7]
)unsigned int time = (m_aHeader[4] << 24) | (m_aHeader[5] << 16) | (m_aHeader[6] << 8) | m_aHeader[7];
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Provided
m_aHeader[k]
(k=4..7) is abyte
, there is no difference, i.e. you may write ( note there is NO0xff
close tom_aHeader[7]
)unsigned int time = (m_aHeader[4] << 24) | (m_aHeader[5] << 16) | (m_aHeader[6] << 8) | m_aHeader[7];
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi there, I got a little confused here, maybe someone can help with this noob question. Following code piece to convert byte to int (supposedly big endian):
unsigned int time = ((m_aHeader[4] & 0xff) << 24) |
((m_aHeader[5] & 0xff) << 16) |
((m_aHeader[6] & 0xff) << 8) |
(m_aHeader[7] & 0xff);Now why is this different than
unsigned int time = ((m_aHeader[4] ) << 24) |
((m_aHeader[5] ) << 16) |
((m_aHeader[6] ) << 8) |
(m_aHeader[7] );m_aHeader is a byte array. Edit: Just found, my problem only occurs when I use the first piece of code in java (in c++ it works fine). I create the bytes in c++ with
m_aHeader[4] = (BYTE) ((time >> 24) & 0xff);
m_aHeader[5] = (BYTE) ((time >> 16) & 0xff);
m_aHeader[6] = (BYTE) ((time >> 8) & 0xff);
m_aHeader[7] = (BYTE) (time & 0xff);If I read it in c++ with the further above mentioned code pieces, everything is fine. If I try this in java, though,
long time = ((m_aHeader[4] ) << 24) |
((m_aHeader[5] ) << 16) |
((m_aHeader[6] ) << 8) |
(m_aHeader[7] );modified on Wednesday, August 26, 2009 3:44 AM
The reason you generally want to and with 0xff is to ensure that when your byte is promoted to int (as it will be internally in the expression), it is zero-extended, i.e. the top 24 bits of the int are set to zero, not one. Why would they be set to one? Basically if the language sign-extends the byte when it makes it an int AND the top bit of the byte is set. Now, if you use 'unsigned char' for BYTE and 'unsigned int', you should be OK. If you used 'signed char' for BYTE, then you could have issues.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
The reason you generally want to and with 0xff is to ensure that when your byte is promoted to int (as it will be internally in the expression), it is zero-extended, i.e. the top 24 bits of the int are set to zero, not one. Why would they be set to one? Basically if the language sign-extends the byte when it makes it an int AND the top bit of the byte is set. Now, if you use 'unsigned char' for BYTE and 'unsigned int', you should be OK. If you used 'signed char' for BYTE, then you could have issues.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Yes, that´s it. Thank´s a lot. Just found out that it was a signed/unsigned issue. Didn´t know, though, why java would make a difference between
int time = data[3];
result is -16 and
int time = data[3] & 0xff;
result is 240 (data[3] being a byte holding 11110000) So thanks again. I love this forum :). Souldrift