C/C++ >> STRANGE ... !
Posted by fatma on 01:54:00 03-17-2003
how to implement a function GET_BYTES(x,n) that extracts a byte(8bits)from a word(x) (32bits)....?
NOTE:just use {+,&,^,!,|,Z,-->}operations without using conditional statemens or loops...!


EXPLANATION:

assume (x=ox... any hex number);

(n)= a decimal number from the user either (0,1,2,3);

if n=0 the returned word should be as..
( 00000.....000Bo) , n=2 word is
(00000.....00B100)..etc.

EXAMPLE:

x=oxBF2AC80A ,

n=0 then -replace 0A to 00- ,
the returned word is ( oxBF2AC800 )

n=1 then -replace C8 to 00-
the returned word is ( oxBF2A000A )

n=2 then -replace 2A to 00-
the returned word is ( oxBF00C80A )
Posted by Yjo on 09:56:00 03-19-2003
Quote:
that extracts a byte(8bits)from a word(x)

A little confusing: it seems from the rest of the message that you mean for the selected byte to be 'masked out'/made null when 'extracts' would normally have equivalent meaning to 'returns'...

Anyway, without trying any of the code myself, some code which might work with tweaking is:
Code:
unsigned long nullify_byte(unsigned long it,int n)
{
return it & ~(0xFF<<8*n);
}


...looking back at your question i see that the bitwise negate (~) isn't in your list of operators (unless it manifests itself as that curious Z operator} in which case you could try:
Code:
return it ^ (it & 0xFF<<8*n);


[ This Message was edited by: Yjo on 2003-03-19 10:02 ]