In C: what does var = x | y | z; mean? - c

So I've recently started working with TI's CC2650 device and am trying to learn how to program it by studying some of their sample applications. I see a lot of variables declared in this format and I have no clue what it means:
var1 = x | y | z;
In the example above, var1 is of type uint8_t.

| is the binary bitwise or operator. For example: 0x00ff | 0xff00 is 0xffff.

bitwise OR operator, so if you have x = 5 (101) y = 8 (1000) and z = 20 (10100), values in parenthesis are binary values so
x | y | z = 101 | 1000 | 10100 = 11101

The operator | in C is known a s bitwise OR operator. Similar to other bitwise operators (say AND &), bitwise OR only operates at the bit level. Its result is a 1 if one of the either bits is 1 and zero only when both bits are 0. The | which can be called a pipe! Look at the following:
bit a bit b a | b (a OR b)
0 0 0
0 1 1
1 0 1
1 1 1
In the expression, you mentioned:
var1 = x | y | z | ...;
as there are many | in a single statement, you have to know that, bitwise OR operator has Left-to-right Associativity means the operations are grouped from the left. Hence the above expression would be interpreted as:
var1 = (x | y) | z | ...
=> var1 = ((x | y) | z) | ...
....
Read more about Associativity here.

Related

C if(var & something) [duplicate]

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.

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;

C Swapping with shifting bits

Here is a program to swap two numbers with out using temporary variable and using shifting operations:
#include <stdio.h>
#include <conio.h>
int main(void)
{
int a,b,i,j;
clrscr();
printf(“Enter two integers: “);
scanf(“%d%d”,&a,&b);
printf(“a=%d,b=%d\n”,a,b);
for(i = 0; i < 16; i++)
{
if((a & (1 << i)) ^ (b & (1 << i)))
{
a = a ^ (1 << i);
b = b ^ (1 << i);
}
}
printf(“a=%d,b=%d”,a,b);
getch();
return 0;
}
My question is what is significance of 1 in this program?
I know the method of xoring that works as follows
a = a^b;
b = a^b;
a = a^b;
but I don't know how above program works?
It toggles each bit if only one is set.
c = a & (1 << i) = true if the ith bit of a is set
d = b & (1 << i) = true if the ith bit of b is set
| c | d | Action | c' | d' |
-------------------------------------
| 0 | 0 | Do nothing | 0 | 0 |
| 0 | 1 | Toggle the bits | 1 | 0 |
| 1 | 0 | Toggle the bits | 0 | 1 |
| 1 | 1 | Do nothing | 1 | 1 |
1 has one bit on the rightmost position set. 1<<i has one bit on place i set. This program loops through each bit, and swaps them if they are different.
a&(1<<i) tests if a has bit i set.
((a&(1<<i))^(b&(1<<i))) tests if bit i in a and b are different.
a=a^(1<<i) toggles bit i.
It's similar to the XOR trick, but swaps only a single bit at a time and only if this bit actually differs in a and b.
1<<i has bit i set to 1 and all other bits 0.
Also, this does not swap two numbers without using a temporary variable. It uses the temporary i.

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