Logical and Bitwise Expressions

Binghamton
University
CS-211
Fall 2016
Logical and Bitwise
Expressions
The truth value will set you free.
1
Binghamton
University
CS-211
Fall 2016
Expression
Constant
Variable
Prefix: +, -, ++, --, !, ~
Unary Operator
Suffix: ++, -Arithmetic: +,-,*,/,%,
Binary Operator
Logical: >,<,>=,<=,==, !=, &&, ||, ^^
(expression)
Bit: &,|,^, <<, >>
Function Invocation
Assignment: =
Assignment Operator: +=, -=, *=, /=, %=, |=, &=, ^=
Ternary Operator: ?
2
Logical
Expression
Binghamton
University
CS-211
Fall 2016
Constant
Variable
Unary Operator
Prefix: !,~
Logical: >,<,>=,<=,==, &&,||
Binary Operator
Bit: &,|,^, <<, >>
(expression)
Function Invocation
Assignment: =
Ternary Operator: ?
3
Binghamton
University
CS-211
Fall 2016
Logical Expressions
4
Binghamton
University
CS-211
Fall 2016
C Numbers and Logic
• Logic deals with two values: True and False
• Numbers have many values
• In C, zero is logically “False”
• Expressions which evaluate to “False” have a value of 0
• In C, every number OTHER than zero is logically “True”
• Both positive and negative numbers are “True”
• Logically, -32,451 and 1 and 345 all are “True”
• Expressions which evaluate to “True” have a value of 1
5
Binghamton
University
CS-211
Fall 2016
Logical (Boolean) Variables
Option 1
Option 2
• Use an integer variable
• Use ‘1’ for true and ‘0’ for false
• Use library: “stdbool.h”
int firstTime=1;
myFunction(firstTime);
firstTime=0;
myFunction(firstTime);
• Includes data type “bool”
• Includes values true/false
#include <stdbool.h>
bool firstTime=true;
myfucntion(firstTime);
firstTime=false;
myfunction(firstTime);
6
Binghamton
University
CS-211
Fall 2016
Example Logical Binary Operators
int x=7; int y=-3; int z=0;
z=(x>y);
z=(x>0) && (y<0);
z=( 6==x );
7
Binghamton
University
CS-211
Fall 2016
Logical Truth Tables
A
F
F
T
T
B
F
T
F
T
A || B
F
T
T
T
A&&B
F
F
F
T
8
Binghamton
University
CS-211
Fall 2016
Condition
• Expression interpreted as a logical value
int x=9;
x>10
x==6
(x-9)
x && (113/x < 12)
9
Binghamton
University
CS-211
Fall 2016
Short Circuit Evaluation
• Given: <cond1> && <cond2>
• If <cond1> evaluates to False, <cond2> is not evaluated!
• Given <cond1> || <cond2>
• If <cond1> evaluates to True, <cond2> is not evaluated!
10
Binghamton
University
CS-211
Fall 2016
Unary Logic Operator
• Only one unary logic operator… ! (not)
• !true => false !false=>true
int x=271;
x = !x;
int y = !y;
What is x? y
Binghamton
University
Binary Logic Operators
• Comparison: <, <=, ==, >, >=, !=
• Warning: Comparison is different than assignment!
int a=16;
if (a=3) printf(“The value of a is %d\n”,a);
if (3==a) printf(“The value of a is 3\n”);
• Also && and ||
CS-211
Fall 2016
Binghamton
University
CS-211
Fall 2016
Ternary operator
<condition> ? <true_expr> : <false_expr>
Evaluate condition…
if true, evaluate <true_expr>
if false, evaluate <false_expr>
y=x?3/x:0; /* If x=7, y=2… if x=0, y=0 */
printf(“B variable is %s\n”,B?”true”:”false”);
hamlet=question?bb:!bb;
13
Binghamton
University
CS-211
Fall 2016
BITWISE Operators
14
Binghamton
University
CS-211
Fall 2016
Logical vs. Bitwise
Logical
BitWise
• The entire variable has a single
truth value
• Logical operators work on truth
values (each operand is T or F)
• A logical operator performs one
logical operation
• Each bit in the variable has a
single truth value
• BitWise operators work on
columns of bits
• A bitwise operators performs
multiple logical operations …
one for each bit position in the
arguments
• Use single operators & | ^
• Use double operators… && ||
15
Binghamton
University
CS-211
Fall 2016
Example
Logical
Bitwise
int x=x0000 FFFF;
int y=xFFFF 00FF;
int z = x && y;
int x=x0000 FFFF;
int y=xFFFF 00FF;
int z = x & y;
x!=0, so
y!=0, so
z = true
z = true
x is true
y is true
&& true
= 1
x=x0000 0000 0000 0000 1111 1111 1111 1111
y=x1111 1111 1111 1111 0000 0000 1111 1111
&----------------------------------------z=x0000 0000 0000 0000 0000 0000 1111 1111
16
Binghamton
University
CS-211
Fall 2016
Bitwise Operators
• ~ - “invert” operator… flips each bit in the argument
• (different from !... logical “not”… inverts the logical value of a variable)
• Unary… all the rest are binary
• & - and operator… 1 if both bits are 1.
• | - or operator… 1 if either bit is 1.
• ^ - exclusive or operator… 1 if bits are different
17
Binghamton
University
CS-211
Fall 2016
Bit Shifting
• Shift Left – Same as multiply by two
signed char x=53;
signed char y=x<<1;
0
0
1
1
0
1
0
1
0
1
1
0
1
0
1
0
1
1
0
1
0
1
0
1
1
0
1
0
• Shift Right – Same as divide by two (almost)
signed char x=53;
sign
0
0
signed char y=x>>1;
0
0
0000….
Binghamton
University
CS-211
Fall 2016
“Bit Twiddling”
• Combination of bitwise operations and shifting
• Enables manipulation of multiple bits
• Often very “clever” (or confusing)
• Examples…
if ((x^y)<0) // do x and y have opposite signs?
for(c=0;v;v>>=1) c+=v&1; // count true bits in v
19
Binghamton
University
CS-211
Fall 2016
Resources
• Programming in C, Chapter 3
• WikiPedia: Operators in C and C++
(https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B)
• Wikipedia: Short Circuit Evaluation
(https://en.wikipedia.org/wiki/Short-circuit_evaluation)
• Wikipedia: Bit manipulation
(https://en.wikipedia.org/wiki/Bit_manipulation)
20