3

Possible Duplicate:
Can I use a binary literal in C or C++?

I cannot display the results of bitwise operators in C. In the code below, a&b should be 100001 and a|b 111111. However, the printed results are different. I tried to do this with and without itoa, but to no avail. Why doesn't the program print the answers correctly?

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

int main (int argc, char* argv[]) {

  unsigned a = 101101;
  unsigned b = 110011;

  unsigned c = a&b;
  unsigned d = a|b;

  char s[100];
  char t[100];

  itoa(c,s,2);
  itoa(d,t,2);

  printf("%s\n",s); /* Shouldn't it produce 100001? 
                       Instead I get 11000100010101001*/
  printf("%s\n",t); /* Ought to print 111111.
                       Instead it prints 11010111111111111 */

  return 0;
}

Thank You

Community
  • 1
  • 1
saad
  • 211
  • 3
  • 13
  • Sounds like you want binary literals, see this question http://stackoverflow.com/questions/2611764/can-i-use-a-binary-literal-in-c-or-c – WildCrustacean Jan 01 '13 at 13:15
  • Since the linked question suggests using Boost C++ library, I would suggest it's not a very good match. – Mats Petersson Jan 01 '13 at 13:18
  • The linked question does cover both C/C++, but it also says that BOOST_BINARY can be used in C. And that's just one answer, there is a lot of other relevant information there. – WildCrustacean Jan 01 '13 at 13:20

3 Answers3

8

The numbers you provide are decimal, while you meant to use binary. Try the following:

unsigned a = 0x2d; // 101101 b
unsigned b = 0x33; // 110011 b

Or for a GCC compiler use:

unsigned a = 0b101101;
unsigned b = 0b110011;
md5
  • 23,373
  • 3
  • 44
  • 93
Eli Iser
  • 2,788
  • 1
  • 19
  • 29
1

You need to express your numbers as decimal (e.g. 101101 becomes 45, 110011 becomes 51), ocatal (101101 becomes 055, 110011 is 063) or hexadecimal (as Eli shows, 0x2d and 0x33 respectively).

Some compilers MAY support binary, but most don't. Working directly in binary is painful anyways once numbers get more than about 8-12 bits long, so learning how to work with binary in either hex (most common these days) or octal (used to be popular in the 1970's, particularly on DEC equipment that used a lot of 3-bit combinations, so it made sense).

You could of course use strtol(str, NULL, 2) to translate a binary string to a long value.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Thank you. I just needed to check code that used bitwise operators. Hex seems the most straightforward way to go. – saad Jan 01 '13 at 13:26
1

The integers you have defined (a and b) are not defined using bits. You have defined integers that are 101101 and 110011. So even with proper display, that would not work.

What I recommend you to get more or less what you want is:

First properly define your number. Some C compilers (as far as I know, this is not standard) define the bit notation, so a would be: 0b101101 and b would b: 0b110011. That way:

unsigned a = 0b101101;

unsigned b = 0b110011;

Otherwise, you have to either define them using hexadecimal notation (0x....) or directly in 10-base. In first case, a would be: 0x2d or 45.

Furthermore, for the display, you have no way to display bits. So, you should to display with hexadecimal notation (which is more appropriate for that kind of things), using: printf("c: %#x", c);

It will output: c: 0x21.

Heis Spiter
  • 351
  • 3
  • 12
  • Since the objective is to display a binary number, I can just convert it to a base two string using itoa and print it. – saad Jan 01 '13 at 13:31