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

C program to perform logic operations

18 posts · originally from https://geek.digit.in/community/threads/c-program-to-perform-logic-operations.173999/

A
Arnab008 Original post #1
May 20, 2013 at 1:18 PM
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.
.jRay. #2
May 20, 2013 at 1:42 PM
For or you can use ¦¦
Example:

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

For and you can use &&
Example:

if (a=b) &&(a=c)
rijinpk1
May 20, 2013 at 2:42 PM
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;
Shah
Shah #4
May 20, 2013 at 5:29 PM
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;

corrected...
rijinpk1
May 20, 2013 at 5:35 PM
I didn't put double &(and) and double |(or).Did you edit my quoted post? and it is not necessary.
Shah
Shah #6
May 20, 2013 at 5:37 PM
I didn't put double &(and) and double |(or).Did you edit my quoted post? and it is not necessary.

AFAIK, using && and II is the correct form, at least in C++. That's why I edited. Correct me if I am wrong.
rijinpk1
May 20, 2013 at 5:41 PM
these are known as bitwise operators and regarding "&&" it is explained by nickaustin in earlier post.:)
harshilsharma63
May 20, 2013 at 6:13 PM
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 |
rijinpk1
May 20, 2013 at 7:19 PM
@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.
harshilsharma63
May 20, 2013 at 7:23 PM
@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.
Wrong quote. Corrected :)
A
Arnab008 #11
May 25, 2013 at 9:29 AM
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.
rijinpk1
rijinpk1 #12
May 25, 2013 at 11:49 AM
Check my #3 post buddy.

Check my #3 post buddy.
skeletor
skeletor #13
May 28, 2013 at 12:31 AM
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
Shah #14
May 28, 2013 at 8:20 AM
&& = 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?
A
Arnab008 #15
Jun 2, 2013 at 9:15 AM
Thank you guys....
A
Arnab008 #16
Jun 4, 2013 at 1:01 PM
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.
skeletor
skeletor #17
Jun 4, 2013 at 2:01 PM
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.
A
Arnab008 #18
Jun 4, 2013 at 7:30 PM
Yeah I know about 2's compliment. I was not aware of the bitwise operation in details. Nyways thanx.