Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C: set bits in long integer

  • 05-09-2010 10:04PM
    #1
    Registered Users, Registered Users 2 Posts: 218 ✭✭


    Like the title says I'm trying to read and set bits in a long integer, here's my code:
     23 typedef long type_t;
     24 //typedef int type_t;
     25 
     26 int main() {
     27         type_t x = 3;
     28         for (type_t i = 0; i < sizeof(type_t) * 8; i++) {
     29                 if (x & (1 << i)) printf("T");
     30                 else printf(".");
     31         }
     32         printf("\n");
     33 }
    

    long: "TT..............................TT.............................."
    int: "TT.............................."

    Why do I get the repeating pattern when reading the long integer?


Comments

  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    Possibly because the 1 in line 29:
    if (x & (1 << i)) printf("T");
    
    isn't a long int, but an int (C won't declare it a long int unless it has to or you tell it to). Try this and see if it fixes it:
    if (x & (1L << i)) printf("T");
    
    But that won't work for int then.
    Perhaps if you had
    type_t foo = 1;
    
    And later used
    if (x & (foo << i)) printf("T");
    
    That might be the better way.


    edit: just tested it and it looks like that was it:
    #include <stdio.h>
    
    typedef long type_t;
    //typedef int type_t;
    int main()
    {
        type_t x = 3;
        type_t foo = 1;
        for (type_t i = 0; i < sizeof(type_t) * 8; i++) {
            if (x & (foo << i)) printf("T");
            else printf(".");
        }
        printf("\n");
    }
    

    You do know that that will only compile in a GNU99/C99 compiler or later, right?


  • Registered Users, Registered Users 2 Posts: 218 ✭✭Tillotson


    Thank you!!

    I spent an embarrassing amount of time flailing around not seeing the source of the problem.


  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    It's just fresh eyes.
    If it makes you feel any better, I lost two hours this week because I didn't notice I had an 'i' instead of a 'j' in an array index inside a loop; in the Monaco console font, the two are annoyingly similar. Got up, walked away, had coffee, came back, sprayed coffee all over screen when I spotted it sitting there, laughing at me...


  • Registered Users, Registered Users 2 Posts: 2,534 ✭✭✭FruitLover


    Sparks wrote: »
    I lost two hours this week because I didn't notice I had an 'i' instead of a 'j' in an array index inside a loop

    Made a similar mistake myself many moons ago using 'i' and 'j' as outer and inner loop counters. Since then, I've systematically used 'ctr', 'ctr_outer' and 'ctr_inner' for loop counters...


Advertisement