How to for() loop with hex numbers?

This is a discussion on How to for() loop with hex numbers? within the C Programming forums, part of the General Programming Boards category; I know, I know, inside the software, a number is a number. But here's my problem... I want to do ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    12

    How to for() loop with hex numbers?

    I know, I know, inside the software, a number is a number. But here's my problem...

    I want to do something like this:

    Code:
    for (i=0; i<=63; i++) {
          if ( ( flags & (1 << i ) ) != 0 ) 
               printf ("bit %i is set\n", i);
         else
               printf ("bit %i is not set\n", i);
    }
    That doesn't work, though, because for example if i is 49, that's interpreted as hex and it tries to get info for bit 73 (49 in hex is 73).

    Of course, I could write 0x31 if I knew when I was writing the code that I wanted to talk to bit 49, but if I'm in a loop, or have an algorithm that might determine which bit I want to talk do, how do I say "interpret this number as hex"?

    In other words, how can I write

    Code:
          if ( ( flags & (1 << somevar ) ) != 0 )
    such that somevar is in interpreted as hex? A lot of the examples I've googled assume we're dealing with an 8-bit char, which is a moot point because 1-8 is the same in either decimal or hex ;-)

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    It will work. Nothing will be interpreted in hex. Like you said, a number is just a number.

    1 << 10

    will shift left by 10 (decimal) bits.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    12
    Quote Originally Posted by cyberfish View Post
    It will work. Nothing will be interpreted in hex. Like you said, a number is just a number.

    1 << 10

    will shift left by 10 (decimal) bits.
    I thought so, too, but...well, perhaps I have a different problem. I am writing code on 32-bit linux. According to all docs I can find, unsigned long long ints are guaranteed to be 64 bits.

    Code:
    $ cat example.c
    #include <stdio.h>
    
    int main (void) {
            unsigned long long int flags;
            int i;
    
            flags = 0;
    
            flags |= (1 << 2);
            flags |= (1 << 21);
            flags |= (1 << 49);
            flags |= (1 << 63);
    
            for (i =0; i<=63; i++) {
                    if ( ( flags & (1 << i ) ) != 0 )
                            printf ("bit %i is set\n",i);
            }
    
    }
    Code:
    $ gcc -o example example.c
    example.c: In function ‘main’:
    example.c:11: warning: left shift count >= width of type
    example.c:12: warning: left shift count >= width of type
    
    Code:
    $ ./example
    bit 2 is set
    bit 21 is set
    bit 34 is set   <- incorrect
    bit 53 is set   <- incorrect
    
    I'd assumed it was a hex notation problem...perhaps it's something else?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Easy way to check:
    Code:
    print("There are %d bits in an unsigned long long.\n", 8*sizeof(unsigned long long int));
    EDIT EDIT EDIT: Although, the catch is, "1" is not an unsigned long long. Your shift should be
    Code:
    1ULL << 49

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    (1 << 63)
    Type of 1 is int, type of 63 is int, so the type of the result will also be int (truncated).

    Then, it will be sign or zero extended to 64-bits and stored into flag.

    Try
    (1LL << 63)

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    12

    Talking

    Quote Originally Posted by tabstop View Post
    Easy way to check:
    Code:
    print("There are %d bits in an unsigned long long.\n", 8*sizeof(unsigned long long int));
    EDIT EDIT EDIT: Although, the catch is, "1" is not an unsigned long long. Your shift should be
    Code:
    1ULL << 49
    Bravissimo! That is it. Replacing "1" with "1ULL" fixed it. Makes complete sense.

    Thanks much - this was quite educational.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. divide 2 hex numbers
    By dust555 in forum C Programming
    Replies: 5
    Last Post: 04-15-2007, 11:20 PM
  2. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 08:15 PM
  3. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 05:37 AM
  4. converting hex to dec
    By jibbles in forum C Programming
    Replies: 20
    Last Post: 08-07-2004, 11:40 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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