# Binary number in C



## Arnab008 (Jun 10, 2013)

How to take a binary number as input in C?


----------



## harshilsharma63 (Jun 10, 2013)

Input it to a string and perform operation on it.


----------



## Arnab008 (Jun 10, 2013)

#include <stdio.h>
int main()
{
    char b[100],s,t;
    scanf("%[^\t\n]s",b); /*scanf to accept multi-word string*/
    s=b[0]|b[1];
    t=b[0]^b[1];
    printf("%c \n%c",s,t);
    return 0;
}

What are the changes that I shall make in the program? I am not getting the desired o/p.


----------



## harshilsharma63 (Jun 10, 2013)

See, when you store something in a 'char' variable, it not the character but it's ASCII value which gets stored (which is an 8-bit positive number). Whenever you perform any operation on the shar, the operation gets performed on the ASCII value stored and not on the actual symbol you stored. So, when you wrote:



> s=b[0]|b[1];
> t=b[0]^b[1];



what was coming in the variables 's' and 't' was the result of or and and on the ASCII values of b[0] and b[1] and not on 0 and 1 (the ASCII value of 0 is NOT 0 and of 1 is not 1).

As I've explained the issue, try yourself to see if you can correct the program.


----------



## Arnab008 (Jun 11, 2013)

As far I know there is no ASCII value for 1 or 0. So what will be the ASCII value that will be stored in the string? Please help me because I cannot find a way out to operate with the bits.


----------



## rijinpk1 (Jun 11, 2013)

0-48
1-49

0-48
1-49


----------



## harshilsharma63 (Jun 11, 2013)

Arnab008 said:


> As far I know there is no ASCII value for 1 or 0. So what will be the ASCII value that will be stored in the string? Please help me because I cannot find a way out to operate with the bits.


The ASCII value of 0 is 48 and that of 1 is 49. Here 0 and 1 are decimal numbers and not binary. There is no direct way of treating a number as binary in C, you have to do it manually.


----------



## Arnab008 (Jun 11, 2013)

Then I think its better to take it in an integer array.


----------



## harshilsharma63 (Jun 11, 2013)

^ that will be highly unefficient and lots of (7 bytes per bit input) memory wastage.


----------



## Arnab008 (Jun 11, 2013)

U r r8. Can u pls correct the program that I had written so that I can operate with the bits considering them as binary 0 and 1. I need all the six operations (OR, NOR, AND, NAND, XOR and XNOR).


----------



## harshilsharma63 (Jun 11, 2013)

Basic (read: unoptimized) code for understanding of error:


```
#include <stdio.h>
int main()
{
char b[100];
int s, t, x, y;
scanf("%[^\t\n]s",b); /*scanf to accept multi-word string*/

if(b[0]=='0')
    x=0;
else
    x=1;

if(b[1]=='0')
    y=0;
else
    y=1;

s=x|y;t=x^y;

printf("%c \n%c",s,t);return 0;
}
```

optimized code:


```
#include <stdio.h>
int main()
{
char b[100],s,t;
scanf("%[^\t\n]s",b); /*scanf to accept multi-word string*/

for(int temp=0; temp<100; ++temp)
    b[temp]-='0';

s=b[0]|b[1];
t=b[0]^b[1];
printf("%c \n%c",s,t);
return 0;
}
```

I hope you understand the problem in your original code.


----------



## Arnab008 (Jun 11, 2013)

Is the optimized code giving you the desired result? I executed it but didn't get the proper result.
void input()
{
    printf("Enter the length of the codeword. \n");
    scanf ("%d",&i);
    printf("Enter the bits of the message starting from MSB to LSB. \n");
    j=i-1;
    while (j>=0)
    {
        scanf ("%d",&digits[j]);
        j--;
    }
}
This is the way how I am taking a binary number as i/p. It is taking a huge memory and the execution time is high. But I can operate with the bits.
Now please tell me how can I do the same thing using less memory and taking a string i/p?


----------



## harshilsharma63 (Jun 11, 2013)

Sorry, there was a mistake:


```
#include <stdio.h>
int main()
{
char b[100],s,t;
scanf("%[^\t\n]s",b); /*scanf to accept multi-word string*/
for(int i=0; b[i]!=NULL; ++i)
    b[i]-='0';

for(int i=0; ++i<digits.size()-1; ++i)
{
        --i;
    s=digits[i]|digits[i+1];
    t=digits[i]&digits[i+1];
    printf("\nOR: %d", s);
    printf("\nAND: %d", t);
        printf("/n");
}
printf("%c \n%c",s,t);return 0;}
```


----------



## Arnab008 (Jun 12, 2013)

Its not working.


----------



## harshilsharma63 (Jun 12, 2013)

Arnab008 said:


> Its not working.


Dude, dont be dumb, at least mention what is the problem occurring. And even if it's not working, the logit of treating the string as binary digits is clearly mentioned in the code. use your brain to correct the code and understand the logic. We are here to suggest and to point the wrong, not to do what's your's work.


----------



## Arnab008 (Jun 12, 2013)

You just provided the modified code. I tried to run it. There were lots of errors. I corrected them and executed the code. But I did not get the proper result. Please dont think that I am just running your code and using that same part in any of my program. My doubt was how to use binary numbers and try to operate them. Without providing the codes its not possible to explain. When you are providing any code, that means that it gives the desired result. There is no point of being dumb or not using the brain. I have done using integers and you know very well that it takes a huge memory.

What is the use of understanding the logic of treating the string as a binary number if it can not be used in any operations. The o/p of the AND, XOR operations were wrong.


----------



## harshilsharma63 (Jun 12, 2013)

Pseudo code:

1. Store binary number in string.
2. Subtract '0' (with quotes) from all elements of string.

Now you can perform any logical operation on any element of string. To print the binary number stored in string, type-cast it to integer while printing, otherwise, symbols will be printed instead of 0s and 1s.

for(int i=0; i<size_of_string; ++i)
string_-='0';_


----------



## Desmond (Jun 12, 2013)

What kind of operation do you plan on doing on the entered Binary number?


----------



## deepakkrishnan (Jun 13, 2013)

Provide a sample input and provide an expected output that you want to achieve from the said input.

Also share tell us what are the errors that occur during compile or run-time.


----------



## Arnab008 (Jun 13, 2013)

That will be a better option. Suppose I take two inputs as 100110. Now the I want to operate the 0th bit of i/p with the 1st bit. And this goes on upto the MSB. The operation can be anything like OR,AND, NOR, NAND, XOR and XNOR. For OR, the desired o/p is 10111. I think now you can understand.

Code - 
#include <stdio.h>
#include <string.h>
int main()
{
char b[100],t;
int i,l=0;
scanf("%[^\t\n]s",b); /*scanf to accept multi-word string*/
for(i=0; b_!='\0';i++)
{
    b-='0';
    l++;
}
for(i=0;i<l-1;i++)
{
    t=b&b[i+1];
    t=!t;
    printf("\nNAND: %d", (t));
    printf("\n");
}
return 0;
}
The program is working good in case of OR,AND and XOR. But its not giving the proper result while using NOT. If I use the bitwise ~ operator, the o/p is in 2's compliment form._


----------



## rijinpk1 (Jun 13, 2013)

Have you put t=~b_ inside the loop?

Have you put t=~b inside the loop?_


----------



## harshilsharma63 (Jun 13, 2013)

Arnab008 said:


> That will be a better option. Suppose I take two inputs as 100110. Now the I want to operate the 0th bit of i/p with the 1st bit. And this goes on upto the MSB. The operation can be anything like OR,AND, NOR, NAND, XOR and XNOR. For OR, the desired o/p is 10111. I think now you can understand.
> 
> Code -
> #include <stdio.h>
> ...


_
Using '!' wouldn't work. Create a function for that._


----------



## Arnab008 (Jun 13, 2013)

Ok I will do that.


----------



## deepakkrishnan (Jun 13, 2013)

Check the image and let me know if this is what you're trying to achieve.

if yes then I believe you can use modulo '%' to achieve all the tasks that you mentioned


----------



## Arnab008 (Jun 14, 2013)

This is the actual thing that I want to do. The o/p will depend on the operator used. Just check it. Its nothing but a simple encoding process.


----------



## deepakkrishnan (Jun 14, 2013)

try this and check I've not compiled it but should work as per my assumptions.

```
char i_number[32];
char o_number[32];
int length, a,b;

printf("Enter the binary number : ");
scanf("%s", i_number);

length = strlen(b_number);


// For AND operation;

for (int i = length-1 ; i>0; i++ )
{
			
	if(i_number[i] == '0')
		a = 0;
	else
		a =1;

	if(i_number[i-1] == '0')
		b = 0;
	else
		b = 1;
	
	a = a&b;
	o_number[i] = a;
}

	
	if(i_number[0] == '0')
		a = 0;
	else
		a =1;

	if(i_number[length-1] == '0')
		b = 0;
	else
		b = 1;
	
	a = a&b;
	o_number[i] = a;
```


----------



## Arnab008 (Jun 14, 2013)

I dont think that this command - "o_number_ = a;" will work. Still I will check it once._


----------



## deepakkrishnan (Jun 14, 2013)

As I said I haven't checked it .. so not sure .. ideally it should take it .. as far as my understanding goes


----------



## Arnab008 (Jun 15, 2013)

#include <stdio.h>
#include <string.h>
int main()
{
char i_number[32];
char o_number[32];
int length, a,b,i;
printf("Enter the binary number : ");
scanf("%s",i_number);
length = strlen(i_number);
for (i = length-1 ; i>0; i--)
{
	if(i_number_ == '0')
		a = 0;
	else
		a =1;
	if(i_number[i-1] == '0')
		b = 0;
	else
		b = 1;
	a = a&b;
	a=!a;
	o_number = a;
}
	if(i_number[0] == '0')
		a = 0;
	else
		a =1;
	if(i_number[length-1] == '0')
		b = 0;
	else
		b = 1;

	a = a&b;
	a=!a;
	o_number = a;
	for (i=0;i<length;i++)
        printf("%d",o_number);
	return 0;
}
This is the complete code. Its working. But the complexity time is high in this case, and if we want to do all the operations, time will increase more._


----------



## deepakkrishnan (Jun 15, 2013)

Arnab008 said:


> a=!a;
> o_number_ = a;
> 
> for (i=0;i<length;i++)
> ...


_

I didn't get why you used "a=!a";
and instead of using a loop to display the output you can directly use "printf("%s" , o_number );

this is the simplest way of implementing .. I am trying to figure out a more efficient way of doing that but it's failing me.. If you're able to crack it then please do share. _


----------



## Arnab008 (Jun 15, 2013)

Yeah the loop for printing the o/p is useless. But how to replace '!'? M trying too. I will let u know if I crack the code in an efficient manner.


----------



## harshilsharma63 (Jun 15, 2013)

@OP; create functions for all logical operations rather than embedding the conde in min itself.


----------



## deepakkrishnan (Jun 15, 2013)

Arnab008 said:


> Yeah the loop for printing the o/p is useless. But how to replace '!'? M trying too. I will let u know if I crack the code in an efficient manner.



Why do you want "a=!a;" what are you trying to achieve with this.. 



harshilsharma63 said:


> @OP; create functions for all logical operations rather than embedding the conde in min itself.



The code i gave was supposed to be implemented in a function.. one can implement 6 functions but that would lead to redundant processing of the following code ... Insights on this are more than welcome


```
for (i = length-1 ; i>0; i--)
{
if(i_number[i] == '0')
a = 0;
else
a =1;
if(i_number[i-1] == '0')
b = 0;
else
b = 1;
```


----------



## Arnab008 (Jun 15, 2013)

Its a simple not operation that I want to perform. If a was 1, a=!a will store 0 in it.


----------



## deepakkrishnan (Jun 15, 2013)

^I got that but y do you want to do that .. any particular reason ..


----------



## Arnab008 (Jun 16, 2013)

Ok. Actually I am working on a program based on crytography. Now to publish a research paper I need to calculate each and every details of it. So I need to calculate for each and every operations.


----------



## deepakkrishnan (Jun 16, 2013)

ohh ok cool .. all the best for your paper


----------



## harshilsharma63 (Jun 16, 2013)

One more memory saving tip. Use C++ instead of C; this will make the Bool data-type available which is the best for the purpose and is just 1 byte long. Plus, you will bet lower abstraction.


----------



## Arnab008 (Jun 16, 2013)

Okay. Thats good bt m limited with a knowledge about C. I will learn C++ very soon. Then I will try to do the program.


----------

