Thread: i've got a problem i can't really comprehend

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    28

    i've got a problem i can't really comprehend

    *read 32 bit number in hexadecimal format
    (printf/scanf)
    the hex number needs to be divided into groups of binary

    eg. hex number is 7a348734
    needs to be divided into groups of 6 bits/5bits/5bits/10 bits/6 bits
    eg. 011110 10001 10100 1000011100 110100

    what would be the best way of going about this do you think?

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    next time, dont start another thread on the same topic we're already helping you on. Please read the faq for that and other rules of courtesy.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You mean like this?
    Code:
    #include <stdio.h>
    
    void show_num_as_bin(unsigned int num, int startbit, int endbit)
    {
      int bit;
    
      for(bit = endbit-1;bit >= startbit-1;bit--)
        printf("%d", num & (1 << bit) ? 1 : 0);
      printf("\n");
    }
    
    int main(void)
    {
      unsigned int num = 0x7a348734;
      int groups[] = { 6, 10, 5, 5, 6 };
      int i, start = 1;
    
      show_num_as_bin(num, 1, 32);
    
      for(i = 0;i < 5;++i)
      {
        show_num_as_bin(num, start, start + groups[i] - 1);
        start += groups[i];
      }
    
      return 0;
    }
    itsme@itsme:~/C$ ./bit
    01111010001101001000011100110100
    110100
    1000011100
    10100
    10001
    011110
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or you can do it like this where num & mask is actually a useful portion of the original number:
    Code:
    #include <stdio.h>
    
    void show_num(unsigned int num)
    {
      int bit;
    
      printf("0x%08X: ", num);
      for(bit = 31;bit >= 0;bit--)
        printf("%d", num & (1 << bit) ? 1 : 0);
      printf("\n");
    }
    
    int main(void)
    {
      unsigned int num = 0x7a348734;
      int groups[] = { 6, 10, 5, 5, 6 };
      int i, j, mask, start = 0;
    
      show_num(num);
      printf("\n");
    
      for(i = 0;i < 5;++i)
      {
        mask = 0;
        for(j = 0;j < groups[i];++j)
          mask |= 1 << j;
        mask <<= start;
    
        show_num(num & mask);
        start += groups[i];
      }
    
      return 0;
    }
    itsme@itsme:~/C$ ./bit
    0x7A348734: 01111010001101001000011100110100
    
    0x00000034: 00000000000000000000000000110100
    0x00008700: 00000000000000001000011100000000
    0x00140000: 00000000000101000000000000000000
    0x02200000: 00000010001000000000000000000000
    0x78000000: 01111000000000000000000000000000
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    And finally, the exact way you showed you wanted it:
    Code:
    #include <stdio.h>
    
    void show_num_as_bin(unsigned int num, int startbit, int endbit)
    {
      int bit;
    
      for(bit = endbit-1;bit >= startbit-1;bit--)
        printf("%d", num & (1 << bit) ? 1 : 0);
    }
    
    int main(void)
    {
      unsigned int num = 0x7a348734;
      int groups[] = { 6, 10, 5, 5, 6 };
      int i, start = 32;
    
      show_num_as_bin(num, 1, 32);
      printf("\n\n");
    
      for(i = 4;i >= 0;--i)
      {
        show_num_as_bin(num, start - groups[i] + 1, start);
        start -= groups[i];
        printf(" ");
      }
      printf("\n");
    
      return 0;
    }
    itsme@itsme:~/C$ ./bit
    01111010001101001000011100110100
    
    011110 10001 10100 1000011100 110100
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Quote Originally Posted by Draco
    next time, dont start another thread on the same topic we're already helping you on. Please read the faq for that and other rules of courtesy.
    sorry. i should have mentioned that this is actually a seperate part.
    in the other thread i need to convert hex to bin.
    in this thread i need to convert again, but also to seperate. and i think that it might be worthwhile doing a different type of conversion..
    but i'm not sure?

    sorry to not make that clear initially..

    thanks for your help.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why is it everyone feels the need to do everything for people? What ever happened to pointing the way and helping with errors?

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Maybe I should have explained my example in your other thread.

    If you have a hex number - 7a348734
    each separate digit is made up of 4 bits (half a byte). I believe its called a nibble?

    Since your wanting each digit separately you can mask.

    Going from right to left (starting at bit 0) you can get just ththose 4 bits like so:

    Code:
    //our hex number
    int num = 0x7a348734
    
    //you could create an array to hold each nibble
    unsigned char nibble[8];
    
    //going from right to left
    nibble[0] = num & 0x0000000F;  // now holds 4 by itself
    nibble[1] = (num & 0x000000F0) >> 4; // now holds 3 by itself
    now the way & works is it compares each bit of the two variables and "returns" 1 if both bits are 1, 0 otherwise

    so if you have the hex number 7ab3 (random hex number)

    that would be 0111101010110011
    then if you and it with 0x000F

    Code:
     0111101010110011
    &0000000000001111
    ---------------------------
     0000000000000011
    and 0000000000000011 = 3

    now as you go on through the number you will need to shift the bits so that the target number is all the way to the right. (so youd shift an extra 4 bits each digit).

    I hope this has helped you understand and not confused you further.

    EDIT:
    I just noticed that you want them in different bit length. But the concepts still apply. By the way, why exactly do you need them in these different lengths?
    Last edited by Vicious; 08-08-2004 at 12:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM