1

The code that I can't understand does this :

int decodeTimeStampByte(final byte timeByte) {
   return timeByte & (~64);
}

So for instance, if I get the byte 4c (which is ASCII L), what exactly would the above function do to it? How about the byte 44?

5
  • This...doesn't seem to do anything useful or meaningful whatsoever. Commented Apr 27, 2012 at 22:13
  • 1
    About "no one knows C", now C is considered the most popular language by the Tiobe Community Index tiobe.com/index.php/content/paperinfo/tpci/index.html Commented Apr 27, 2012 at 22:14
  • ok, breaking this down a bit, what does ~64 do? Commented Apr 27, 2012 at 22:16
  • If you remove the final this would be C and would do the same thing, you might need to brush up on your C Edit and the byte -> char or something Commented Apr 27, 2012 at 22:18
  • @Musa Considering that c doesn't have byte and that c's char is usually unsigned (although afaik not guaranteed by the spec) - not necessary (although in this case yes I think). In C it'd be the usual way to clear a bit.. although usually you'd write that as x & ~(1 << bitToClear) for documentation. Commented Apr 27, 2012 at 22:24

4 Answers 4

3

The '~' is bitwise 'not', so 64 = 0x40 = 0100000b and ~64 = 1011111b (the lower 5 bits set).

Then '&' is bitwise 'and' and it leaves just the 5 lower bits of timeByte. So, basically, it is a truncation of timeByte to 0..63 range.

decodeTimeStampByte(4c) = 0xC (12)

decodeTimeStampByte(44) = 44

P.S. Yes, I forgot the higher bits. ~64 = 1011111b.

It is either a bug in the code or some intention to leave the sign bit (the 7-th bit) in place.

P.P.S. Seems like an ancient bit-hack to squeeze some more performance

Sign up to request clarification or add additional context in comments.

3 Comments

Also be aware of negative byte values as input. The final range will not always be 0..63.
Yes, it all seems like a direct borrowing from an old C codebase :) And if a person had a Pascal background, then the 'signedness' of the byte may not come to mind.
yeah thx rico - i verified the domain of values that need to be decoded in this bizarre way (only part of the spec where required to do this) are all under 127 so that will things to 6 bits and less. so the sign bit thing people mentioned wont be a factor.
2

This code will clear the bit 6. But if the bit 7 is set, it will set all bits from 8 to 31 (due to casting byte to int)

3 Comments

why is that? according to other answers, ~64 is 011111B. so wouldnt bit 7 also be 0, as in 0011111B? 0 & 1 = 0. how does bit 7 get set here?
It's only inverting 64, which always leads to a mask of 00011111b. It's not inverting the variable timeByte. A quick Java test confirms that 127 as input leads to 63 as a result.
try with 255 (when the bit 7 is set) and see what is the result. Note, that bits are counted from 0 (not 1).
1

This function is returning the lower 6 bits for positive values and clearing the 7th bit for negative values. So, 2^6=64, 64 = 1000000 in binary, ~64 = 0111111 in binary would mask values between [0..63] and [-128..-65] of timeByte.

1 Comment

Actually 64 = 1000000b and ~64 = 0111111b, also does byte in java only have 7 bits, what about the 8th?
0

This function is returning the lower 6 bits for positive values and clearing the 7th bit for negative values. So, 2^6=64, 64 = 1000000 in binary, ~64 = 0111111 in binary would mask values between [0..63] and [-128..-65] of timeByte.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.