Logical and Bitwise Expressions

Similar documents
Logical and Bitwise Expressions

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

JAVA OPERATORS GENERAL

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Operators. Java operators are classified into three categories:

Operators in java Operator operands.

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

A flow chart is a graphical or symbolic representation of a process.

Chapter 3: Operators, Expressions and Type Conversion

SECTION II: LANGUAGE BASICS

Arithmetic and Bitwise Operations on Binary Data

Arithmetic and Bitwise Operations on Binary Data

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

C-string format with scanf/printf

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

Computers Programming Course 6. Iulian Năstac

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Operators in C. Staff Incharge: S.Sasirekha

1.3b Type Conversion

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

Applied Computer Programming

Operators & Expressions

C-string format with scanf/printf

Programming. Elementary Concepts

Chapter 4. Operations on Data

BITS, BYTES, AND INTEGERS

Expressions and Precedence. Last updated 12/10/18

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

CS 253. January 14, 2017

Expression and Operator

Bits, Words, and Integers

Integer Encoding and Manipulation

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Prof. Navrati Saxena TA: Rochak Sachan

Arithmetic Operators. Portability: Printing Numbers

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

ISA 563 : Fundamentals of Systems Programming

Operators And Expressions

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

CT 229. Java Syntax 26/09/2006 CT229

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Operators and Expressions

Beginning C Programming for Engineers

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Operators and Expressions:

CSCI2467: Systems Programming Concepts

Data III & Integers I

CS Programming I: Branches

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

OBJECT ORIENTED PROGRAMMING

Lecture 3 Operators MIT AITI

Bits and Bytes. Bit Operators in C/C++

Bit Manipulation in C

UNIT 3 OPERATORS. [Marks- 12]

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Hardware: Logical View

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Prepared by: Shraddha Modi

CSE I-Sem)

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Part I Part 1 Expressions

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Data III & Integers I

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Lecture 02 C FUNDAMENTALS

Data III & Integers I

Unit 3. Operators. School of Science and Technology INTRODUCTION

AGENDA Binary Operations CS 3330 Samira Khan

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Why embedded systems?

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Fundamental of Programming (C)

More Programming Constructs -- Introduction

Boolean algebra. June 17, Howard Huang 1

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

QUIZ: What value is stored in a after this

Representing and Manipulating Integers Part I

LECTURE 3 C++ Basics Part 2

Course Topics - Outline

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Department of Computer Science

Programming Project 2

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

Bitwise Data Manipulation. Bitwise operations More on integers

Bits, Bytes and Integers

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Binary Values. CSE 410 Lecture 02

The Java language has a wide variety of modifiers, including the following:

Engineering Computing I

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Lexical Considerations

Fundamentals of Programming

Transcription:

Logical and Bitwise Expressions The truth value will set you free. 1

Expression Constant Variable Unary Operator Binary Operator (expression) Function Invocation Assignment: = Prefix: +, -, ++, --,!, ~ Suffix: ++, -- Arithmetic: +,-,*,/,%, Logical: >,<,>=,<=,==,!=, &&,, ^^ Bit: &,,^, <<, >> Assignment Operator: +=, -=, *=, /=, %=, =, &=, ^= Ternary Operator:? 2

Logical Expression Constant Variable Unary Operator Prefix:!,~ Binary Operator (expression) Logical: >,<,>=,<=,==, &&, Bit: &,,^, <<, >> Function Invocation Assignment: = Ternary Operator:? 3

Logical Expressions 4

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

Logical (Boolean) Variables Option 1 Use an integer variable Use 1 for true and 0 for false int firsttime=1; myfunction(firsttime); firsttime=0; myfunction(firsttime); Option 2 Use library: stdbool.h Includes data type bool Includes values true/false #include <stdbool.h> bool firsttime=true; myfucntion(firsttime); firsttime=false; myfunction(firsttime); 6

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

Logical Truth Tables A B A B A&&B F F F F F T T F T F T F T T T T 8

Condition Expression interpreted as a logical value int x=9; x>10 x==6 (x-9) x && (113/x < 12) 9

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

Unary Logic Operator Only one unary logic operator! (not)!true => false!false=>true int x=271; x =!x; int y =!x; What is x? y

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

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

BITWISE Operators 14

Logical vs. Bitwise Logical 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 Use double operators && BitWise 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 & ^ 15

Example Logical int x=x0000 FFFF; int y=xffff 00FF; int z = x && y; Bitwise int x=x0000 FFFF; int y=xffff 00FF; int z = x & y; x!=0, so x is true y!=0, so y is true z = true && true z = 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

Logical vs. Bitwise Hardware (8 bit) Logical Z = X && Y Bitwise Z = X & Z

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 18

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 0000. Shift Right Same as divide by two (almost) signed char x=53; signed char y=x>>1; sign 0 0 1 1 0 1 0 1 0 0 0 1 1 0 1 0

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 20

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