Reputation: 59
in ruby.h, I find it difficult to understand this macro:
#define SYMBOL_P(x) (((VALUE)(x)&0xff)==SYMBOL_FLAG)
I have no idea why this &
operation is executed with 0xff
. Doesn't every number & 0xff
equal itself?
Upvotes: 0
Views: 214
Reputation: 87476
The VALUE type in the Ruby source code is usually 32-bit or 64-bit, so the & 0xFF
sets all the bits except the lowest 8 to 0.
Upvotes: 0
Reputation: 2184
& is a bitwize operator (AND), (remember logic table?)
0 & 0 = 0
1 & 0 = 0
0 & 1 = 0
1 & 1 = 1
so what it does here?
0xff is the hexa of 255
in binary (DWORD): 00000000 00000000 00000000 11111111
so assuming a number x= any_value
the representation of x can be like this
???????? ???????? ???????? ????????
each ? can be either 1 or 0
so applying bitwize operator & (AND) with the mask 0xff gives
???????? ???????? ???????? ????????
&
00000000 00000000 00000000 11111111
=
00000000 00000000 00000000 ????????
for example
00000000 00000000 00000011 00000011
&
00000000 00000000 00000000 11111111
=
00000000 00000000 00000000 00000011
^________________________^ ^______^
zeroed kept
Upvotes: 1