2

I have this code, I'm trying to port from php to c/objective-c:

if ($byteIndex < count($data) ) {
    $light = ( ( ($data[$byteIndex] >> $bitIndex) & 1) == 1);
}

But I can't seem to find anywhere what the >> is indicating here. nor the "& 1", for that matter.

Marc B
  • 356,200
  • 43
  • 426
  • 500
Kenny Winker
  • 11,919
  • 7
  • 56
  • 78
  • 2
    Those are [bitwise operators](http://php.net/manual/en/language.operators.bitwise.php). I'd explain what they do, except I have no clue. They are to me a black art of inscrutable design. – sdleihssirhc Apr 01 '11 at 01:43
  • 1
    possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Jon Apr 01 '11 at 01:46
  • @sdl You're thinking too high level there. The answer is 101010. :o) – deceze Apr 01 '11 at 01:54

1 Answers1

8

Bitwise operators - Shift Right and And :)

http://php.net/manual/en/language.operators.bitwise.php

http://en.wikipedia.org/wiki/Bitwise_operation

$score = 2295;

echo((($score >> 2) & 1) == 1)? "1": "^1"; // 1
echo((($score >> 3) & 1) == 1)? "1": "^1"; // ^1

The question is what are you shifting and how many bits? Is it something with colors?

Using & and >> to convert hexadecimal to RGB (decimal).

$hex = 0xCCFF33; // my favourite :)

$r = $hex >> 16;
$g = ($hex & 0x00FF00) >> 8;
$b = $hex & 0x0000FF;

printf("rgb(%d,%d,%d)", $r, $g, $b); // rgb(204,255,51)

This is what happens: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fbitshe.htm

Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66