1

I found a MySQL Query in my application, which I really don´t understand. Its very long, this is just the part of it, I dont understand:

 WHERE ( id & 0xff = 0 ) OR ( ( id >= 3000 ) AND ( id & 0xff != 0xff ) )

What the heck is this '0xff'? Threre is no column '0xff' in this table.

Tream
  • 1,049
  • 2
  • 13
  • 29
  • 1
    It's just a hexadecimal number. – Sirko Aug 15 '15 at 19:05
  • Thank you for your reply. Why is this in the Query? 0xff != 0xff makes no sense at all for me (since its always false....). – Tream Aug 15 '15 at 19:06
  • Have you seen this? http://stackoverflow.com/questions/11380062/what-does-value-0xff-do-in-java – THK Aug 15 '15 at 19:08
  • 1
    That `&` is not the logical AND, but the binary one! – Sirko Aug 15 '15 at 19:08
  • I really don´t understand what it is doing exactly. But you are right, I get different results, when I remove the '& 0xff != 0xff' part. – Tream Aug 15 '15 at 19:12
  • With parenthesis, you have to read it like `( id & 0xff ) != 0xff`. It basically reduces `id` to the last 8 bits and makes sure, they are not all set. – Sirko Aug 15 '15 at 19:14

1 Answers1

2

0xff binary value is 00000000000000000000000011111111 (under the 32-bit integer)

If you do a bitwise AND with 0xff, it gives only the value in the last 8 bits, all other bites vanishes( because 1 & 0 = 0 and 0 & 0 = 0)

id & 0xff = 0

this will check the last 8 bits are 00000000;

id & 0xff != 0xff

this will check the last 8 bits != 11111111;

Praveen
  • 8,945
  • 4
  • 31
  • 49