# c programming / binary question

This is a discussion on c programming / binary question within the C Programming forums, part of the General Programming Boards category; is there a simple way to declare a line of zeros and ones as binary code in c? i have ...

1. ## c programming / binary question

is there a simple way to declare a line of zeros and ones as binary code in c? i have written a program that converts integers to binary code but it is a character string. i am trying to write a program that takes two integer inputs, converts them to binary and then calculates the sum.

2. What do you mean by converting ints to binary? An int is already in binary format in the computer hardware. If you're referring to the representation of the number in text form, that's something different than what the number actually is.

Don't get confused into thinking the same number is different if it's in different formats. It's all the same if it's an int.

3. this what you're looking for?

Code:
```int n = 112314;
int b = 0;
for (int j=0; j<16; ++j)
{
b = (n>>j)&0x00000001;
printf("&#37;d", b);
}```
?

lol, you know, to add two binary numbers this is what you do;

so i bet what you want to do is store int as binary values (within an array of int), then write an algorithm to perform the task of an adder?

then you OR mask (this char, '|') all the array values in to a new int.

i don't know why you want to go through the trouble to do this conceptual action yourself, but i'm going to assume it's for learning purposes. :-)

what you might have wanted to say is you want to simulate binary addiction in C, that would clear some confusion for others.

4. It might be easier to add the two numbers together, and then print the sum out as binary.

To convert it back the other way, consider
Code:
```const char *binary = "1010100";
long number = strtol(binary, NULL, 2);```
where 2 is base 2. (You can supply 0 and strtol(), from <stdlib.h>, tries base 8 (leading 0), base 16 (leading 0x), and base 10.)