in reply to Re^2: Shift Operators And Bitwise Operators
in thread Shift Operators And Bitwise Operators
In a 32-bit int,
$core_dumped = ($? >> 15) & 1; 00000000 00000000 CSSSSSSS EEEEEEEE >> 15 00000000 00000000 00000000 0000000C & 1 00000000 00000000 00000000 00000001 = 1 00000000 00000000 00000000 0000000C
Ok, everything to the left of "C" should always be zero, so the & 1 doesn't do anything. Me bad. The signal one is a better example:
$signal = ($? >> 8) & 127; 00000000 00000000 CSSSSSSS EEEEEEEE >> 8 00000000 00000000 00000000 CSSSSSSS & 127 00000000 00000000 00000000 01111111 = 127 00000000 00000000 00000000 0SSSSSSS
The shift repositioned the bits of interest and removed the lower-precision bits we didn't want.
The "and" removed the higher-precision bits we didn't want.
You could do the operations in the opposite order:
$signal = ($? & 0x7F00) >> 8; 00000000 00000000 CSSSSSSS EEEEEEEE & 0x00007F00 00000000 00000000 01111111 00000000 = 0x00007F00 00000000 00000000 0SSSSSSS 00000000 >> 8 00000000 00000000 00000000 0SSSSSSS
The "and" removed all the bits we didn't want.
The shift repositioned the bits of interest.
You should be start by understanding how machines store integers.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Shift Operators And Bitwise Operators
by biohisham (Priest) on Jun 22, 2009 at 23:50 UTC |