in reply to Bitwise and operator question
Just taking the first of those problem lines should make it clear to you:
print ((128&$bin)/128);
This takes your number in $bin and bitwise-ANDs it with 128 (27 = 128). The result of that will be either 128 or 0. Divide that result by 128 and you get zero or one.
Repeat for 64 (26) down to 1 (20) and you have your binary number.
Take 123, for example
| 123 | & | 128 | = | 0 | / | 128 | = | 0 |
| 123 | & | 64 | = | 64 | / | 64 | = | 1 |
| 123 | & | 32 | = | 32 | / | 32 | = | 1 |
| 123 | & | 16 | = | 16 | / | 16 | = | 1 |
| 123 | & | 8 | = | 8 | / | 8 | = | 1 |
| 123 | & | 4 | = | 0 | / | 4 | = | 0 |
| 123 | & | 2 | = | 2 | / | 2 | = | 1 |
| 123 | & | 1 | = | 1 | / | 1 | = | 1 |
update: added the example
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Bitwise and operator question
by citromatik (Curate) on Aug 09, 2007 at 14:48 UTC | |
by Severy (Initiate) on Aug 09, 2007 at 15:26 UTC | |
|
Re^2: Bitwise and operator question
by Severy (Initiate) on Aug 09, 2007 at 14:46 UTC | |
by FunkyMonk (Bishop) on Aug 09, 2007 at 14:55 UTC | |
by mr_mischief (Monsignor) on Aug 09, 2007 at 15:12 UTC |