Thread: Help Please...bits

  1. #1
    Unregistered
    Guest

    Question Help Please...bits

    I am trying to do an exhaustive search to find a key. That key is 32bits, but I have reduced this search by finding out which key bits are effective (ie: effecting the output).

    I have therefore eliminated the a good deal of bytes from this exhaustive search, leaving me with 12 bits of possibilites for the remaining bits. 2^12 is much less than 2^32.

    I now need some way to cycle through the possibilities for the remaining 12 bits.

    Here's an example:
    from left to right: bit0 -> bit31

    key={XXXXXXXX XX000000 XX000000 XXXXXXXX}

    "bits set to X is a don't care"

    I need a way to get all possible values for the above key.

    key={XXXXXXXX XX000000 XX000001 XXXXXXXX}
    key={XXXXXXXX XX000000 XX000010 XXXXXXXX}
    key={XXXXXXXX XX000000 XX000011 XXXXXXXX}
    key={XXXXXXXX XX000000 XX000101 XXXXXXXX}

    all the way to
    key={XXXXXXXX XX111111 XX111111 XXXXXXXX}

    Does anybody have any ideas as I've been trying things which are not working......
    thank you!!!!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you can use bitwise and & to test bits.
    char a, c=123;
    a= c&1; // state of least significant bit
    a=c&2;// state of next bit
    a=c&4; // etc
    a=c&8;
    a=c&16; // and so on.

    You can set bits by bitwise orring | with true value in the bit you want to turn on.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    is there any easier way I could cycle through all values ...

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    of course there is. just test lsb and then right shift.then test again.then right shift.etc.
    work on a copy of your key rather than the key itself.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Unregistered
    Guest

    Angry

    i'm kinda of a newbie at C ...
    Could you get me started with a snipet of code please....

  6. #6
    Unregistered
    Guest
    Here's my attempt but it's returning over 10,000 possible values for these key bits...when there should only be 2 to the power of 12=4096....

    for ( x = 0 ; x <= 0x000f0f00 ; x += 0x100 )
    {
    for ( y = 0 ; y <= 0x03f ; y++ )
    {
    for ( z = 0 ; z <= 0x03f ; z++ )
    {
    key= x | y | z;
    counter++;
    printf("\nSomething found ::%d", key);
    printf("\nNumber ::%d", counter);
    }
    }
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    A post which has a remarkable resemblance to
    http://www.cprogramming.com/cboard/s...&threadid=7923

  8. #8
    Unregistered
    Guest
    yeah..that was me too but the bits have changed again and I dont understand the logic...
    i tried that code with three for loops but it's returning over 10,000 keys...
    any ideas???

  9. #9
    Unregistered
    Guest
    Use Stoned_Coder's idea but instead of using 0's and 1's just use their hex or decimal equivalent. For instance to find every possible bit combo in a 16-bit usigned int just do a for (unsigned int i=0;i<0xFFFF;i++).

    It just counts to 0xFFFF but it will cover every single bit combo that is possible in those 16-bits for the unsigned int. Also you could code an arithmetic shift right and left - remember to preserve the bit that is shifted out of the number so you can properly rotate the bits. But, this will not give you every combo for the bits.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > key={XXXXXXXX XX000000 XX000000 XXXXXXXX}
    Written as
    key={XXXXXXXX XXAAAAAA XXBBBBBB XXXXXXXX}

    You need a loop variable for each contiguous block of bits (one for AAAAAA and one for BBBBBB


    Then you look at the least significant bit of each of them, and this tells you by how much to increment that counter by
    So
    for BBBBBB, the increment is 0x100
    for AAAAAA, the increment is 0x10000

    The limit of each is all those bits set
    So
    for BBBBBB, the limit is 0x3F00
    for AAAAAA, the limit is 0x3F0000

  11. #11
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >key={XXXXXXXX XX000000 XX000000 XXXXXXXX}

    Byte 0 and byte 3 are don't cares. Only bytes 1 and 2 are relevant. The first two bits of both bytes are don't cares. Am I correct?

    An inefficient way would be: take bytes 1 and 2 and then count from 0x0000 to 0xFFFF. It's inefficient since you're taking the first two bits of both bytes into account. But it's a way.

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yeah, I didn't say it was the best way. Looks like Salem comes through again - looks like my cookies aren't logging me in

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  2. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  3. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  4. byte is equal 8 bits???
    By Micko in forum C Programming
    Replies: 3
    Last Post: 10-15-2004, 10:12 AM
  5. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM