C if(var & something) [duplicate] - c

I am lacking some basic understanding in bitwise '&' operator.
5 = 101
4 = 100
So why the output of the below if condition is true cause and of bits 101 & 100 should be false:
#include <stdio.h>
main()
{
if(5&4)
printf("Yes\n");
}

5 is 101.
4 is 100.
5 & 4 is not 0:
101
100 &
↓↓↓
100
Problem solved ✓
Clarification:
In C, every non-zero value satisfies the if condition. Meaning, if you write:
if (-5) {
if (100) {
// reachable code
}
}
Whereas:
if (0) {
destroyTheWorld(); // we are safe
}

5 - 101
4 - 100
5&4 - 100
It is true.

Understanding bitwise operator truth tables is crucial. Consider the following, where A and B are inputs and Y is the output.
& (Bitwise And) When inputs A and B are true, output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1
| (Bitwise Or) When A or B or both inputs are true output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
^ (Bitwise X-Or) When A and B are opposite states, output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
! (Bitwise Not) Output is the opposite state of the input
A Y
-----
0 | 1
1 | 0
Your Equation (5 & 4) == (0101 & 0100) == 0100 == 4 == true
0101
& 0100
------
0100

Because 0b100 & 0b101 equals 0b100 and the latter does not equal 0.

0b101 & 0b100 = 0b100
or
5&4 = 4
and 4 is non-zero and prints Yes

It enters the if condition. Because after the & operation it returns non-zero value. In C, for all non-zero value it's like returning true.

Related

Is not(A XOR B) the same as (!A XOR !B)?

I would like to know if !(A xor B) is equal to (!A xor !B)?
I am struggling to understand the logic behind this problem.
They are not equal. You could check the following table for further explanation.
+---+---+-------+--------+----+----+-------+
| A | B | (A^B) | !(A^B) | !A | !B | !A^!B |
+---+---+-------+--------+----+----+-------+
| 0 | 0 | 0 | 1 | 1 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 | 0 | 0 | 0 |
+---+---+-------+--------+----+----+-------+
Edit: Computing !(A^B) without using NOT operation with A, B, A' and B'
XOR(A, B) = OR(AND(A, B'), AND(A', B))
After using DeMorgan for the equation above:
NOT XOR(A,B) = AND(OR(A', B), OR(A, B'))
If in doubt, use truth tables. A and B can be 1 or 0 so:
A xor B:
0 1
1 0
! (A xor B)
1 0
0 1
! A xor ! B:
0 1
1 0
So, the answer is no. They seem to be the same as the initial xor.
Going step by step, and looking at the resulting column, we see that they do not result in the same output based on the same input.
A
B
A XOR B
not(A XOR B)
0
0
0
1
0
1
1
0
1
0
1
0
1
1
0
1
A
B
!A
!B
(!A XOR !B)
0
0
1
1
0
0
1
1
0
1
1
0
0
1
1
1
1
0
0
0
No they're not.
A xor B is equal to 1 if and only if either A or either B is 1 but not both. Therefore !(A xor B) is equal to 1 if and only if both A and B are equal.
Whereas with (!A xor !B) you first flip the bits and then do the XOR. So (!A xor !B) = (A xor B).
Here is the truth table for the first one:
A | B | A xor B | !(A xor B)
============================
0 | 0 | 0 | 1
0 | 1 | 1 | 0
1 | 0 | 1 | 0
1 | 1 | 0 | 1
and for the second one:
A | B | !A | !B | (!A xor !B)
=============================
0 | 0 | 1 | 1 | 0
0 | 1 | 1 | 0 | 1
1 | 0 | 0 | 1 | 1
1 | 1 | 0 | 0 | 0

use ((c1^c2) & ~32) to test if c1 and c2 are the same character in different case

I saw some code like this
if( ((c1^c2) & ~32)==0 )
{
...
}
In this snippet the code likely mean that if the if statement is true, then c1 and c2 are the same character in different case, meaning that one of those is +32 or -32 away from the other. Why is that?
I did test myself and discover that in some case it is true while in others not:
printf("%d", (65^97)& ~32); //output is 0. right
printf("%d", (97^65)& ~32); //output is 0. right
printf("%d", (50^82)& ~32); //output is 64!! not the same though 82-50=32
Why is that? what is the magic in it?
(c1^c2) & ~32) xors c1 and c2, the result contains the bits that are in both characters and & with ~32 clears (ignores) the bit 5. (It is zeroed whether it was same in both or not). Comparing this with zero, checks if all the bits other than bit 5 are same.
This can be used to check if 2 letters are equal ignoring their case in ascii representation if you are sure that atleast c1 or c2 is a valid latin character(a-z, A-Z).
To understand this, let's pick 2 characters with different case and compare them:
+---+---+---+---+---+---+---+---+
a | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
+---+---+---+---+---+---+---+---+
| x | | | | | |
+---+---+---+---+---+---+---+---+
A | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
a ^ A | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
32 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
~32 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
& | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
You can try the same with j v/s J or t v/s z.
So there is no magic involved, only this logic.
Sometimes this condition is also written as:
if (ch1 == ch2 || (ch1 ^ 32) == ch2)
{
...
}

Bitwise '&' operator

I am lacking some basic understanding in bitwise '&' operator.
5 = 101
4 = 100
So why the output of the below if condition is true cause and of bits 101 & 100 should be false:
#include <stdio.h>
main()
{
if(5&4)
printf("Yes\n");
}
5 is 101.
4 is 100.
5 & 4 is not 0:
101
100 &
↓↓↓
100
Problem solved ✓
Clarification:
In C, every non-zero value satisfies the if condition. Meaning, if you write:
if (-5) {
if (100) {
// reachable code
}
}
Whereas:
if (0) {
destroyTheWorld(); // we are safe
}
5 - 101
4 - 100
5&4 - 100
It is true.
Understanding bitwise operator truth tables is crucial. Consider the following, where A and B are inputs and Y is the output.
& (Bitwise And) When inputs A and B are true, output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1
| (Bitwise Or) When A or B or both inputs are true output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
^ (Bitwise X-Or) When A and B are opposite states, output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
! (Bitwise Not) Output is the opposite state of the input
A Y
-----
0 | 1
1 | 0
Your Equation (5 & 4) == (0101 & 0100) == 0100 == 4 == true
0101
& 0100
------
0100
Because 0b100 & 0b101 equals 0b100 and the latter does not equal 0.
0b101 & 0b100 = 0b100
or
5&4 = 4
and 4 is non-zero and prints Yes
It enters the if condition. Because after the & operation it returns non-zero value. In C, for all non-zero value it's like returning true.

homework: Trying to sort out what operation I am attempting to compute C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Sorry for asking you guys and not my professor. (its due in a couple of hours and she is not available)
but I am just looking for a point in the right directions.
one piece of what I am writing tonight states two things...
Single-character versions of the C logical operators:
& for && (logical AND)
| for || (logical OR)
! for logical NOT
and this... as example output.
Enter an expression: (0 & 1) | (1 & 1)
Result: (0 & 1) | (1 & 1) = true
My pseudo logic is to take the input of '&' or '|' and have it return as '&&' or '||' and throw it all back together based on the entered expression and let the program do the math.
But what I don't understand is what would make the above expression evaulwate to true? and or false? What should I research to lean more about the above expression? and what makes it true or false?
The concept of true and false in C is of integers 0 for false and a non-zero number for true.
"single" boolean operators like &, |, ~ and ^ are called bitwise operators.
They work on two numbers, bit by bit - following the logic tables respectively - AND, OR, ONE-COMPLEMENT(not) and XOR.
So, your expression:
(0 & 1) | (1 & 1) is true because 0 & 1 = 0, 1 & 1 = 1 and 0 | 1 is 1. which is true.
What could turn this into false is if the expression was combined with an & instead of | like so:
(0 & 1) & (1 & 1) = false.
Truth table of AND:
+---------------+---------+
| A + B | A & B |
+---------------+---------+
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
+---------------+---------+
Truth table of OR:
+---------------+---------+
| A + B | A | B |
+---------------+---------+
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
+---------------+---------+
However, since C99, true and false are reserved words, and will evaluate to 0 and 1 internally - The answer posted above applies for C99 as well as C89
Look at it this way:
(0 & 1) | (1 & 1)
= (0 and 1) or (1 and 1)
= (false and true) or (true and true)
= (false) or (true)
= true
Further reading at wikipedia
Thanks for everyone's input. Really helped.
int logicalCheck (int a, int x, int b) {
// let 88 = true
// let 89 = false
if (x == '&') {
if (a == b) {
return 88;
}
if (a != b) {
return 89;
}
}
if (x == '|') {
if ((a == b && a) || b != 0) {
return 88;
}
if (a || b == 0) {
return 89;
}
}
return 0;

If I have a truth table, is there a way of determining the bitwise expression I need for that truth table?

I am attempting to do the following in c:
unsigned int mask;
unsigned int previous;
unsigned int new;
unsigned int out;
for( int i = 0; i < 8; ++i )
{
bool bit_set = GET_BIT( mask, i );
// If the mask bit is true, use the new bit, otherwise use the previous bit
SET_BIT( out, i, GET_BIT( bit_set ? new : previous, i ) );
}
However I think there may be an easier and quicker way using bitwise operations. I have the truth table but I don't know how to get the expression I need.
Truth table is:
m | p | n | o
0 | 0 | 0 | 0
1 | 0 | 0 | 0
0 | 1 | 0 | 1
1 | 1 | 0 | 0
0 | 0 | 1 | 0
1 | 0 | 1 | 1
0 | 1 | 1 | 1
1 | 1 | 1 | 1
How would I go about working this out?
Use Karnaugh Map - there is a solver available online. Pick "three values", enter the expected results for all eight combinations, and use the expression the solver produces:
F(m, p, n) = (p & !n) | (m & n)
EDIT : You can expand this solution to do the whole byte at once, rather than doing it one bit at a time, by using the ~ bitwise NOT operator:
result = (mask & new) | (~mask & previous);
If the mask bit is true, use the new bit, otherwise use the previous
bit
The natural way to express this (to me) is (mask & new) | (~mask & previous). That is to say, mask corresponding bits from new and previous, and add them together using OR.

Resources