Binary number in C

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.
 

deepakkrishnan

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

Arnab008

Broken In
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

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

Arnab008

Broken In
#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

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

Arnab008

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

deepakkrishnan

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

Arnab008

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

Arnab008

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