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

Simple C question

Options
  • 13-03-2008 12:18am
    #1
    Closed Accounts Posts: 12,382 ✭✭✭✭


    I have a char array like this -

    unsigned char name[10];

    If I wanted to find the 57th bit in name, would I have to go to name[7] and then read the first bit in that byte (i.e. the first bit in the eight byte is bit 57), or can I just look up the 57th bit in name? If so, how can I do this?

    Any help appreciated.

    Thank you.


Comments

  • Registered Users Posts: 413 ✭✭ianhobo


    For all intents and purposes, your array can be operated upon as if its one long contiguous piece of memory, even though it may actually not be.

    But how do you propose to
    just look up the 57th bit in name

    I would imagine that to do that (directly access) you would need a variable that was at least 64bits long in which to hold a mask. Then using a 64bit pointer cast against the chars, you could directly look it up. Is that how you see it?

    Personally, i'd go with an array lookup at index[x], and then perform a mask against that.

    The other way gets into potentially strange and undefined territory. 64-bits data types are not as well defined across platforms as other data types.


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Hi Ian

    Thanks for your reply.

    Yes I was planning on using a 64 bit array as a mask.

    So you recommend if I am looking for bit 57, I use name[7] and use a mask one byte long?

    Do you know how I can on the fly find out what byte a bit is in? For example, how can I calculate bit 57 is in byte 8? Or bit 1 is in byte 1? I have an array with a lot of bit positions in it so I would need to calculate what byte to lookup before using the mask.

    Thanks


  • Registered Users Posts: 413 ✭✭ianhobo


    hey,

    well if you've loads of different bits to set up, you will have to either generate in code the relevant masks, or else hand write them as hash defines. Also, your array would limited in size to equal that of the largest pointer type available to you - most likely a double

    But using the array lookup will be easier, and quicker I think (assuming your on a 32bit machine)

    if you know the bit position you want to look up, then just divide that number by 8 to get the array index
    
    Array Diagram:
    0       8         16        24      32       40
     --------------------------------------
    |  0    |   1     |   2     |   3    |   4    |
     --------------------------------------
    
    for example, if you want the 17th bit, its in byte[2] as above
    so (17 / 8) = 2 remainder 1
    
    to then find which bit, you the % modulus operator
    
    17 % 8 = 1 so its the msb.
    
    example 2:
    
    for the 31st bit:
    31/8 = 3 -> index[3]
    
    31 % 8 = 7
    
    

    So putting all of that into code would go something along the lines of:
    #define BYTESIZE  (8)
    #define FALSE       (0)
    
    //Order reversed to reflect a bitstream
    #define MASK8     (00000001b)
    #define MASK7     (00000010b)
    .
    .
    .
    #define MASK1     (10000000b)
    
    void myFunction(int bitToCheck)
    {
        unsigned char array[10];
      
        bool isBitSet = FALSE; 
    
        int byteToIndex = 0;
       
        int byteToCheckForBit = 0;
    
        int bitToCheck = 0;
    
        char  maskToUse = 0;
    
    
       bytoToIndex = (bitToCheck / BYTESIZE);
    
      //If bitToCheck is 33, then byteToIndex = (33 / 8) = 4
    
       byteToIndex = name[byteToIndex];
    
       //Find the bit - the remainder
       bitToCheck = (bitToCheck % BYTESIZE) 
    
       
       //Similarly, and array lookup would be more elegant here
       //Assign the correct mask to use
       if(bitToCheck == 1)
       {
           maskToUse = MASK1;
       }
       else if(bitToCheck == 2)
       {
           maskToUse = MASK2;
       }
      
      //...add the rest of the cases 
      
        //Check if bit set
        if( byteToIndex & maskToUse)
        {
              isBitSet = true;
              //Bit is set :)
             printf("Bit is set");
         }
    
    }
    

    Sorry, that should work, but its off the top off my head, so I havn't tested it.


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    That's brilliant Ian, thank you very much for your help :)


  • Registered Users Posts: 413 ✭✭ianhobo


    No worries :)


  • Advertisement
  • Registered Users Posts: 413 ✭✭ianhobo


    oh, yeah, is this related to your distributed des project? :D
    Hows that going?


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    :)

    Sort of!

    It's been years since I properly used C, or done any "real" programming (I do web stuff), so it's slowly slowly so far. I'm in no major hurry, so I'm not too worried, yet!


  • Registered Users Posts: 413 ✭✭ianhobo


    ok, cool :)

    When I did my masters we had a cryptography module, so I have DES code and example data files. We had to break it for our CA if remember crrectly, so I should be able to route it out at some stage if you want some code/examples etc


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Slightly off topic, but it's a good link: Bit Wizardry.

    Jorg Arndt is writing a book "Algorithms for Programmers", which available for download above. Loads of low level bit twiddling stuff there.


  • Registered Users Posts: 272 ✭✭Goofy


    Hi guys. I have a similar question. It would be great if any of ye could help me.
    I have an unsigned char array looking something like this {128|50|0|32|60|150|0|0}. What im am looking for is to take out the first 6 numbers i.e. 128, 50, 0, 32, 60, 150. The array is liabe to change to something like this{23|0|0|32|60|150|0|0|10|0} ie the amount of numbers in the array can change and the number of digits in each number can also change.
    I hope i have explained this right as i am a mechanical engineer by trade not a programmer.

    Thanks in advance


  • Advertisement
  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    @Goofy:

    does the array have to grow dynamically, or is there a max size that won't be exceeded?
    like, would 256 unsigned chars be enough?


  • Registered Users Posts: 272 ✭✭Goofy


    Thanks for the reply AJ
    Not entirely sure what the max size is but 256 should be enough. (The network is down at the moment so i cant check) Im only interested in the first 25 or so characters so does that mean i can ignore the rest?

    The array represents the status of a component on a network and changes in real time


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    if all you need is 25 chars, then not much point in copying anymore than this..unless the status of the component requires you to copy all data sent.

    if a string is terminated by \r\n\0 and you need to read line by line...different situation - post some code if you can.

    thanks.


Advertisement