DIGIT-ARC read-only archive · snapshot: July 23, 2026

Binary number in C

39 posts · originally from https://geek.digit.in/community/threads/binary-number-in-c.174706/

A
Arnab008 Original post #1
Jun 10, 2013 at 12:16 PM
How to take a binary number as input in C?
harshilsharma63
Jun 10, 2013 at 1:46 PM
Input it to a string and perform operation on it.
A
Jun 10, 2013 at 7:11 PM
#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 at 8:24 PM
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.
A
Jun 11, 2013 at 8:50 AM
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 at 10:37 AM
0-48
1-49

0-48
1-49
harshilsharma63
Jun 11, 2013 at 11:03 AM
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.
A
Jun 11, 2013 at 11:43 AM
Then I think its better to take it in an integer array.
harshilsharma63
Jun 11, 2013 at 12:04 PM
^ that will be highly unefficient and lots of (7 bytes per bit input) memory wastage.
A
Arnab008 #10
Jun 11, 2013 at 3:10 PM
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 at 6:38 PM
Basic (read: unoptimized) code for understanding of error:

PHP:
#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:

PHP:
#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.
A
Arnab008 #12
Jun 11, 2013 at 8:52 PM
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 at 9:15 PM
Sorry, there was a mistake:

PHP:
#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;}
A
Arnab008 #14
Jun 12, 2013 at 8:42 AM
Its not working.
harshilsharma63
Jun 12, 2013 at 11:03 AM
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.
A
Arnab008 #16
Jun 12, 2013 at 2:28 PM
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 at 3:35 PM
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
Desmond #18
Jun 12, 2013 at 5:24 PM
What kind of operation do you plan on doing on the entered Binary number?
deepakkrishnan
Jun 13, 2013 at 2:27 AM
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.
A
Arnab008 #20
Jun 13, 2013 at 11:30 AM
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
rijinpk1 #21
Jun 13, 2013 at 12:13 PM
Have you put t=~b inside the loop?

Have you put t=~b inside the loop?
harshilsharma63
Jun 13, 2013 at 12:55 PM
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.

Using '!' wouldn't work. Create a function for that.
A
Arnab008 #23
Jun 13, 2013 at 1:48 PM
Ok I will do that.
deepakkrishnan
Jun 13, 2013 at 7:48 PM
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

ip and op.JPG
A
Arnab008 #25
Jun 14, 2013 at 8:51 AM
ip.jpg
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 at 5:56 PM
try this and check I've not compiled it but should work as per my assumptions.
Code:
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;
A
Arnab008 #27
Jun 14, 2013 at 10:56 PM
I dont think that this command - "o_number = a;" will work. Still I will check it once.
deepakkrishnan
Jun 14, 2013 at 11:13 PM
As I said I haven't checked it .. so not sure .. ideally it should take it .. as far as my understanding goes
A
Arnab008 #29
Jun 15, 2013 at 9:32 AM
#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 at 5:28 PM
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.


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. :)
A
Arnab008 #31
Jun 15, 2013 at 5:55 PM
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 at 6:03 PM
@OP; create functions for all logical operations rather than embedding the conde in min itself.
deepakkrishnan
Jun 15, 2013 at 7:22 PM
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..

@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

Code:
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;
A
Arnab008 #34
Jun 15, 2013 at 10:01 PM
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 at 11:46 PM
^I got that but y do you want to do that .. any particular reason ..
A
Arnab008 #36
Jun 16, 2013 at 10:42 AM
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 at 5:12 PM
ohh ok cool .. all the best for your paper :)
harshilsharma63
Jun 16, 2013 at 5:20 PM
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.
A
Arnab008 #39
Jun 16, 2013 at 6:43 PM
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.