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
    Great, I think I am getting it somehow, albeit in a random way... I can summarize this as follows
    #working in octets of bits. 01-shift $! bits to the right. #zeroing on target 02-spread &ing carpet under the target #2**(No.of bits) #carpet size is (1,4,8,16,32) that covers 1,2,3,4..etc bits #from the second bit the relation is (2**2, 2**3, #2**4...2**32) i.e. +2**(bit position). 03-&ing makes us extract the bits we want #(shifted bits)*(appropriate carpet size)=(C or SSs or EEs)
    does this seem all there is to it??? p.s. I don't know anything about carpets by the way except that they are spread or pinned on to a wall