in reply to Shift Operators And Bitwise Operators
# bitfield format: xxxMMMxx my @msgs = ( 'a' .. 'i' ); my $msg = $msgs[ ( $bitfield >> 2 ) && 7 ];
It's also used in doing checksums, hashing, encryption, doing exponent changes in fixed-point arithmetic, quick multiplications and divisions by powers of two, etc, etc.
Update: I missed that you also asked about bitwise operators. They are usually used to extract informations from numbers that contains multiple flags or fields. For example, system returns a number that consists of 3 fields:
bit15 bit0 | | v v C SSSSSSS EEEEEEEE C = Core dumped? S = Signal that caused program to exit E = Process's exit code.
These can be extracted as follows:
$core_dumped = ($? >> 15) & 1; $signal = ($? >> 8) & 127; $exit_code = $? & 255
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Shift Operators And Bitwise Operators
by biohisham (Priest) on Jun 22, 2009 at 18:31 UTC | |
by ikegami (Patriarch) on Jun 22, 2009 at 19:47 UTC | |
by biohisham (Priest) on Jun 22, 2009 at 23:50 UTC | |
by lostjimmy (Chaplain) on Jun 22, 2009 at 19:06 UTC |