# C program to perform logic operations



## Arnab008 (May 20, 2013)

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. (May 20, 2013)

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)

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 (May 20, 2013)

rijinpk1 said:


> And
> z=x*&&*y;
> or
> z=x*||*y;
> ...



corrected...


----------



## rijinpk1 (May 20, 2013)

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


----------



## Shah (May 20, 2013)

rijinpk1 said:


> 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)

these are known as bitwise operators and regarding "&&"  it is explained by nickaustin in earlier post.


----------



## harshilsharma63 (May 20, 2013)

Shah said:


> 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)

@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)

rijinpk1 said:


> @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


----------



## Arnab008 (May 25, 2013)

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 (May 25, 2013)

Check my  #3 post buddy.

Check my  #3 post buddy.


----------



## ico (May 28, 2013)

Shah said:


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



```
#include <stdio.h>

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


```
#include <stdio.h>

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


----------



## Shah (May 28, 2013)

ico said:


> Spoiler
> 
> 
> 
> ...



Thanks for correcting.



Spoiler



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


----------



## Arnab008 (Jun 2, 2013)

Thank you guys....


----------



## Arnab008 (Jun 4, 2013)

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 (Jun 4, 2013)

Arnab008 said:


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



-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 complimen*t? 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.


----------



## Arnab008 (Jun 4, 2013)

Yeah I know about 2's compliment. I was not aware of the bitwise operation in details. Nyways thanx.


----------

