[CST-2] CompArch

William R Sowerbutts will@sowerbutts.com
Sun, 20 May 2001 18:35:49 +0100


On Sun, May 20, 2001 at 06:26:18PM +0100, garan wrote:
>Is anyone else out here not a C freak? ok, so it's just me who has never
>seen it before...
>
>1st bit of code on p59:
>
>p is a pointer to the required byte then? does a bitwise AND with -3, does
>some shift or other and then a bitwise & with 255.
>
>someone care to translate this into english, for the benefit of the crap?

>> unsigned int temp;
>> temp = *(p&~3);

~3 = complement of three, ie binary inverse of 011 => 111...1100
p&~3 = p AND ~3 = mask p with complement of 3 = set bottom two bits of p to 0.
*(p&~3) = load the uint pointed to by p&~3

basically "align p" (ie, set low two bits to 0) and load a uint from that
address.

>> temp = temp >> ( (3-(p&3)) * 8 );

p&3 = low two bits of p
'>>' is the shift right operator

(3-(p&3)) is the number of bytes you need to shift, but >> works in bits, so
you multiply by 8 (actually the compiler would turn this multiply by 8 into a
shift left by 3)

basically this figures out which byte you meant and shifts right by the
appropriate number of bits so that the byte you wanted is in the low 8 bits of
temp.

>> reg = temp & 255

This masks off the low 8 bits of temp (because the rest may contain other
parts of the word you loaded) and stores it in reg, which is your result.


Hope this slightly demystifies it.

Will

_________________________________________________________________________
William R Sowerbutts (BtG)                            will@sowerbutts.com
Coder / Guru / Nrrrd                                http://sowerbutts.com
       main(){char*s=">#=0> ^#X@#@^7=";int c=0,m;for(;c<15;c++)for
         (m=-1;m<7;putchar(m++/6&c%3/2?10:s[c]-31&1<<m?42:32));}