--- In [email protected], "Rayne" <[EMAIL PROTECTED]> wrote:
>
> Hi,
> Is there a way to extract the k most significant bits of
> a 64-bit integer, i.e. if k = 3, and the integer is
> 1011100...001 in binary, then I get the first 3 bits 101
> and read that as 5? I thought of using the right-shift
> bitwise operator >>, but that works only for 8-bit
> integers, right?

It should work for every integer data type your compilers supports.
Otherwise it's not allowed to call itself a C compiler, only to be
named as a "compiler which compiles something which looks like C but
actually isn't".

So don't worry, it should work. Just be careful to declare your 64-bit
integer as "unsigned", otherwise you _may_ have to deal with negative
numbers, and then things become complicated: if I recall correctly (I
only recall that with the original K&R C this was the case), it's
undefined how ">>" behaves when being used on negative integers: at
least with K&R conformant software a compiler was allowed to define
for itself whether a negative number as an operand for ">>" the
negative sign would be kept or not.

So: delare your 64-bit integer as "unsigned", then you should be fine.

Regards,
Nico

Reply via email to