Re: Shift 'em bits

From: Don (none_at_given)
Date: 11/19/04


Date: Fri, 19 Nov 2004 08:30:07 -0700

WaldemarIII <bleek004@hotmail.com> wrote in message
news:419dc904$0$46988$cd19a363@news.wanadoo.nl...
>
> Is there an atomic way to shift 64 bits either left or right?

Do you truly mean "atomic"? (i.e. an operation whose intermediate
results can NEVER be seen -- AS IF uninterruptible) Or, do you
just mean something that you can write in a simple C expression?

> Shifting a double (8 bytes) doesn't work. Compiler won't

The language won't allow it. Shift operators only apply to integer
data types (int, short, char, etc.)

And, if you were expecting to cast some "random" group of 64 contiguous
bits to a double, they might get *altered* in the process (since not all
groups of 64 bits represent valid doubles -- assuming your doubles *are*
64 bits!)

> allow it. Embedded assembly is out of the question and

Why?

> and a C function conating a loop shifting the individual
> bytes consumes too much time.

Also, there be dragons there. If you are presumably aspiring to
write portable code (hence the reason you rule out embedded
assembly), you have to be very careful about how you have
defined that "64 bit datum" (if, in fact, it *is* a 64 bit datum!)
E.g., char's aren't *guaranteed* to be 8 bits (read the standard
or argue the point with a language guru) -- though, chances are, your
compiler treats them as such.

But, you might find that you have defined an "8 uchar array" as your
"64 bit datum", yet, you try to manipulate it as a "2 ulong array".
Does your compiler ensure that char arrays are aligned in a manner
compatible with the alignment it (or the CPU!) expects for *longs*?

You also have to be wary of any right shift operations since
the language gives no assurances as to *what* gets shifted in
from the left.

Etc.

If you really are trying to write portable code (avoiding ASM for
some reason), use the manifest constants in <limits.h> and
conditionally chose the widest UNSIGNED data type you can.
Define your "64 bit datum" as an array of those and build
"monitors" to manipulate data of this (64 bit) type -- i.e. pretend
you are dealing with *objects*.

> Suggestions... Love to hear from you...

Use ASM and forget it! :-/

--don



Relevant Pages

  • Re: << and >> operators in c#
    ... of the array ... the general rule being: ... And what language might that be defined in? ... I think it makes a poor bit shift because it ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: << and >> operators in c#
    ... of the array ... the general rule being: ... Just because you personally have not used a powerful element of the language ... They shift bits either left or right. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)
  • Re: Packages and returning errors
    ... The perldoc function guide says about shift (which is ... "Shifts the first value of the array off and returns it, ... If an array of values are passed to a sub, ... Back to my package (which I am currently thinking might be out of my depth, ...
    (comp.lang.perl.misc)
  • Re: MS SQL geek wants to jump ship, plz help on first Perl script
    ... Next step is the MEAT inside the loop. ... Then you don't understand shift(). ... automatically works on the @_ array (ie, ... # a lot of memory, coz it reads the whole arrary into memory first, ...
    (comp.lang.perl.misc)