Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C: set bits in long integer

  • 05-09-2010 9: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