Suppose I want to find 2nd bit in binary equivalent of 13 (binary : 1101). It should return 0.
views:
125answers:
2
+5
A:
http://php.net/manual/en/language.operators.bitwise.php
($x >> 1) & 1
Andrey
2010-04-15 08:26:24
And function to get nth bit:function nbit($number, $n) { return ($number >> $n-1) }
Tomasz Struczyński
2010-04-15 08:32:19
stereofrog
2010-04-15 10:12:30
@stereofrog: you're right, but for nth bit, you've to find out pow(2, n-1) which is again done by shifting.
understack
2010-04-17 10:43:32