-2

I stumbled upon this weird problem.

I got 2 pair of four octets.

1) 255.255.255.0 (like a subnet mask)
2) 128.127.0.0 (fictive mask)

My binary representation would be:

1) 11111111.11111111.11111111.00000000
2) 10000000.01111111.00000000.00000000

But from my simple application I get:

1) 11111111.11111111.11111111.00000000
2) 11111110.01111111.00000000.00000000

I think that is the wrong value, but please correct me, I think I have misunderstood something important in this.

The code for generating the binary representation is:

#include <stdio.h>
#include <stdlib.h>

void PrintBinary(unsigned int n)
{
    unsigned int tmp;
    int i = 1;
    tmp = n;
    do{
       printf("%d", (n & tmp) ? 1 : 0);
       if(i % 8 == 0 && i < 32)
           printf(".");
       ++i;
       n >>= 1;
      }while(n>0);
    printf("\n");
}

int main(int argc, char **argv)
{
    unsigned int octet1 = 128;
    unsigned int octet2 = 127;
    unsigned int octet3 = 0;
    unsigned int octet4 = 0;
    unsigned int a = octet1 << 24 | octet2 << 16 | octet3 << 8 | octet4;
    PrintBinary(a);
    return 0;
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
OMG-1
  • 498
  • 1
  • 6
  • 20
  • You should find what you need here: http://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format – David Heffernan Oct 08 '12 at 19:02
  • Could you be more specific as the link you provided contains how to print binary, that is not my problem if you tested my code as it will print the binary representation of a number, but when it comes to the binary representation when the number is packed then the output is weird in my understanding. – OMG-1 Oct 08 '12 at 19:06
  • You didn't show enough code. Show a complete program. Don't make us guess. – David Heffernan Oct 08 '12 at 19:08
  • See for yourself now, I've edited the code so you can run it on your computer, but please explain what you mean by letting you guess ? as I clearly showed the result I expected and the result I got from the provided code. I know you couldn't cut and paste the code but in it self it was a functional code. – OMG-1 Oct 08 '12 at 19:13
  • Your problem is the code that prints binary. You should have tested that first. – David Heffernan Oct 08 '12 at 19:18

3 Answers3

2

Your primary problem is

tmp = n;

so in general, you are checking if any of several bits are set in (n & tmp) ? 1 : 0.

Your mask should have only one bit set, and you should shift the mask, not the value.

void PrintBinary(unsigned int n)
{
    unsigned int tmp = 1 << (sizeof(unsigned int) * CHAR_BIT - 1);
    int i = 1;
    do{
       printf("%d", (n & tmp) ? 1 : 0);
       if(i % 8 == 0 && i < 32)
           printf(".");
       ++i;
       tmp >>= 1;
      }while(tmp > 0);
    printf("\n");
}

should provide the desired output.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
1

Your PrintBinary function doesn't make much sense. You are using tmp as a mask, but tmp is equal to n!

Instead, try something like

int i;
for(i=31; i>=0; i--) {
    printf("%d", !!(n & (1<<i)));
    if(i%8 == 0) printf(".");
}
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • I will go and accept yours five liner as accepted. Mostly because I didnt need to reverse the string. I saw now what I was missing in my code, to loop up to 31 bits :P thanks – OMG-1 Oct 08 '12 at 21:16
0

Try:

void PrintBinary(unsigned int n)
{
    unsigned int tmp = n;
    for (int i = 1; i <= 32; ++i) {
       printf("%d", (tmp & 1));
       if (i % 8 == 0)
           printf(".");
       temp >>= 1;
    }
    printf("\n");
}

You can't use a do loop if your termination condition is (n > 0), or you will be missing leading zeros in the first octet when the value of that octet is less than 0x80 (128).

bohney
  • 1,187
  • 8
  • 12