C program to perform logic operations

Arnab008

Broken In
I am not well aware whether operations like or, and, nand, nor, xor and xnor can be performed using any command or operator in C? Please help me guys. Performing the checking conditions for each operation is a hectic and lengthy one and I need to use the operations in another program.
 

.jRay.

Youngling
For or you can use ¦¦
Example:

if (a<b) ¦¦ (a=c)
{
Body
}

For and you can use &&
Example:

if (a=b) &&(a=c)
 

rijinpk1

Aspiring Novelist
And
z=x&y;
or
z=x|y;
xor
z=x^y;
for compliment operations, use "~"(negate) like ~z will perform nand operation if z=x&y;
 

rijinpk1

Aspiring Novelist
I didn't put double &(and) and double |(or).Did you edit my quoted post? and it is not necessary.
 

rijinpk1

Aspiring Novelist
these are known as bitwise operators and regarding "&&" it is explained by nickaustin in earlier post.:)
 
AFAIK, using && and II is the correct form, at least in C++. That's why I edited. Correct me if I am wrong.
Using double & or double is not necessary. See the code below:

int a=-1, b=0;


if(++a&&++b);

cout << a << " " << b << endl;


a=-1;
b=0;
if(++a&++b);

cout << a << " " << b << endl;

the output is:

0 0
0 1

The difference between && and & is that, if the first argument of && yeilds to false, the second argument is not evaluated. But in case of &, both arguments are evaluated, irrespective of the result yield. Same is the case with |
 
Last edited:

rijinpk1

Aspiring Novelist
@harshil,I didn't get you. Op requires AND operation of two binary numbers which cant be done 'directly' using double and(&&) and we should use single and,& as i given in my previous post. && is for condition checking.
 
OP
A

Arnab008

Broken In
Guys I dont wanna know the difference between && and &. I just want to perform simple and, or, xor and the other operations without checking each time whether they are same or different.
 

ico

Super Moderator
Staff member
AFAIK, using && and II is the correct form, at least in C++. That's why I edited. Correct me if I am wrong.
&& = logical AND.
& = bitwise AND

|| = logical OR.
| = bitwise OR.

^ = bitwise XOR.

Bitwise operators = operating on binary bits.

a = 2|1;

This means 10|01

10
01 (use the OR gate bit by bit)
----
11

11 in decimal system is 3.

If you'll print 'a' now, you'll get 3.

-------------------------------------------------------------

On the other hand,

a = 2||1;

Either of the values need to be true for a to have a true value. A true value means 1. False value means 0.
Both are true values. 2 is also true. 1 is also true. Result will be a true value. a will be equal to 1.


PHP:
#include <stdio.h>

int main()
        {
        int a;
        a = 2|1;
        printf("%d\n",a);
        return 0;
        }
Output: 3

PHP:
#include <stdio.h>

int main()
        {
        int a;
        a = 2||1;
        printf("%d\n",a);
        return 0;
        }
Output: 1
 

Shah

Cyborg Agent
&& = logical AND.
& = bitwise AND

|| = logical OR.
| = bitwise OR.

^ = bitwise XOR.

Bitwise operators = operating on binary bits.

a = 2|1;

This means 10|01

10
01 (use the OR gate bit by bit)
----
11

11 in decimal system is 3.

If you'll print 'a' now, you'll get 3.

-------------------------------------------------------------

On the other hand,

a = 2||1;

Either of the values need to be true for a to have a true value. A true value means 1. False value means 0.
Both are true values. 2 is also true. 1 is also true. Result will be a true value. a will be equal to 1.


PHP:
#include <stdio.h>

int main()
        {
        int a;
        a = 2|1;
        printf("%d\n",a);
        return 0;
        }
Output: 3

PHP:
#include <stdio.h>

int main()
        {
        int a;
        a = 2||1;
        printf("%d\n",a);
        return 0;
        }
Output: 1

Thanks for correcting.

I really miss the "Reps" option now. How about adding it to TDF?
 
OP
A

Arnab008

Broken In
I am working on a program in Cryptography. It is better to say that I have designed a new algorithm. I want to implement it in C. Can anyone give ideas how to create the 'keys'?

@rijinpk1 - ~ (not) operation is not working. Please suggest something. If I do ~1 the output obtained is -2.

int main()
{
int a=~1;
printf("%d",a);
return 0;
}
This is the program. The o/p is -2 and when ~0 is performed the o/p is -1.
 

ico

Super Moderator
Staff member
I am working on a program in Cryptography. It is better to say that I have designed a new algorithm. I want to implement it in C. Can anyone give ideas how to create the 'keys'?

@rijinpk1 - ~ (not) operation is not working. Please suggest something. If I do ~1 the output obtained is -2.

int main()
{
int a=~1;
printf("%d",a);
return 0;
}
This is the program. The o/p is -2 and when ~0 is performed the o/p is -1.

-2 is infact the right answer. This is the answer which you should actually expect as you are using the BITWISE NOT operator.

What you are using is the bitwise NOT operator. This is not the logical NOT operator! Emphasis on the exclamation mark. Had it been ! then you would have got zero.

a was earlier 1.

or I should say, it was 00000000000000000000000000000001. (Note: These are 32 bits in total.)

Then you did a NOT gate to every digit, so it became 11111111111111111111111111111110. This is actually Two's compliment of the number 2. This is the representation of -2.)

Have you heard about Two's compliment? This is used to represent negative numbers in computers.

Two's compliment is basically flipping all digits reverse (0 to 1 and 1 to 0) and then adding 1.

So, how would we get the original positive represenation from Two's Compliment? We'll get it by subtracting 1 and then flipping all digits.

Now, 11111111111111111111111111111110 is some negative number i.e. it is the Two's Compliment of some positive number. Let's go reverse.

First I'll subtract 1 from it. I get 11111111111111111111111111111101.

Now I will flip all digits, I get 00000000000000000000000000000010. This means TWO. 11111111111111111111111111111110 is negative representation of 2. It is -2.
 
Top Bottom