Jump to content

Bitwise OR problem


jeffg

Recommended Posts

I have this code, with an echo test statement:

		$jar_mask = $from_ar[5] | $to_ar[5];		echo "jar_mask: $jar_mask; from_ar[5]: {$from_ar[5]}; to_ar[5]: {$to_ar[5]}<br>";

And this is what I get in the output:

jar_mask: 72; from_ar[5]: 32; to_ar[5]: 4

When I last looked, bit5 (32) or'd with bit2 (4) gave bit5|bit2, not bit7|bit3. The result should be 36, not 72.Please tell me I am not going crazy, and I am doing something wrong?Uh-oh: if I change that to

		$jar_mask = intval($from_ar[5]) | intval($to_ar[5]);

then it works. But please could you explain why the first one doesn't. Is it doing some weird string thing?

Link to comment
Share on other sites

Yeah, it's probably doing a string conversion. 72 in binary is 1001000, so it's masking the 8 and 64 bits. I'm not sure why those are both double (left-shifted 1 place) what they should be, but I would assume it's a conversion. But, when you're working with bits, it's always best to explicitly cast all of your variables so that you know what the data types are.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...