Bitshift on 64-bit integers.

This is a discussion on Bitshift on 64-bit integers. within the C Programming forums, part of the General Programming Boards category; Originally Posted by quzah Code: uint64_t shift(uint64_t to_shift, int pos) { if( pos > 32 ) { to_shift <<= 32; ...

  1. #16
    Registered User
    Join Date
    Feb 2005
    Posts
    54
    Quote Originally Posted by quzah
    Code:
    uint64_t shift(uint64_t to_shift, int pos) {
        if( pos > 32 )
        {
            to_shift <<= 32;
            pos -= 32;
        }
        return to_shift << pos;
    }
    Something like that perhaps?

    Quzah.

    My fix was to switch from lcc-32 to gcc. It now recognizes shifts using variables denoting the amount to shift, shrinking my code by alot.

  2. #17
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by maththeorylvr
    but uint_64 doesnt shift correctly when I try to use a variable to represent the number of bits to shift
    I built the following with lcc-win32 and it generated the expected results. It shifted by an amount contained in a variable, and the amount could be greater than 32.
    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    uint64_t shift(uint64_t value, int bits)
    {
       return value << bits;
    }
    
    int main( void )
    {
       uint64_t value = ~0ULL;
       int bits;
       for ( bits = 0; bits < 64; ++bits )
       {
          uint64_t result = shift(value, bits);
          printf("shift(%016"PRIx64", %2d) = %016"PRIx64"\n", value, bits, result);
       }
       return 0;
    }
    I'm only mentioning this because...
    Quote Originally Posted by maththeorylvr
    My fix was to switch from lcc-32 to gcc. It now recognizes shifts using variables denoting the amount to shift
    ...I get nervous about blaming a compiler for my woes; more often there is something I missed in the original code. FWIW.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. A few questions about 64 bit and Windows...
    By yaya in forum Tech Board
    Replies: 9
    Last Post: 08-28-2008, 08:49 AM
  3. 32 bit or 64 bit allignment ?! gcc options??
    By mynickmynick in forum C Programming
    Replies: 3
    Last Post: 07-29-2008, 02:43 AM
  4. 64 bit testing
    By DrSnuggles in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2007, 02:20 AM
  5. Writing 64 bit value in hex to a string
    By rahulgbe in forum C Programming
    Replies: 2
    Last Post: 03-18-2006, 12:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21