Binary number in C

OP
A

Arnab008

Broken In
#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.
 
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.
 
OP
A

Arnab008

Broken In
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.
 
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.
 
OP
A

Arnab008

Broken In
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).
 
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.
 
OP
A

Arnab008

Broken In
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?
 
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;}
 
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.
 
OP
A

Arnab008

Broken In
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.
 
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';
 

deepakkrishnan

Broken In
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.
 
OP
A

Arnab008

Broken In
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.
 
Top Bottom