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

Arithmetic and Bitwise Operations on Binary Data

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:

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

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

The Arithmetic Operators

Beginning C Programming for Engineers

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

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

Chapter 4. Operations on Data

SECTION II: LANGUAGE BASICS

Arithmetic and Bitwise Operations on Binary Data

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

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Programming. Elementary Concepts

Chapter 3: Operators, Expressions and Type Conversion

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

Arithmetic Operators. Portability: Printing Numbers

Bits, Words, and Integers

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

Applied Computer Programming

Bit Manipulation in C

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

Computers Programming Course 6. Iulian Năstac

Binghamton University. CS-211 Fall Data Conversion. software diversity in action

Operators. Java operators are classified into three categories:

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

Real Time & Embedded Systems. Final Exam - Review

Expression and Operator

CS 253. January 14, 2017

C-string format with scanf/printf

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

Operators in java Operator operands.

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

Operators in C. Staff Incharge: S.Sasirekha

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

Reserved Words and Identifiers

Prof. Navrati Saxena TA: Rochak Sachan

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

Conditional Statement

Part I Part 1 Expressions

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

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

C-string format with scanf/printf

ISA 563 : Fundamentals of Systems Programming

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Expressions and Precedence. Last updated 12/10/18

4. Number Representations

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CT 229. Java Syntax 26/09/2006 CT229

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

Integer Encoding and Manipulation

Operators and Expressions:

1.3b Type Conversion

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

COMP 122/L Lecture 2. Kyle Dewey

Representing and Manipulating Integers Part I

CS Programming I: Branches

Here n is a variable name. The value of that variable is 176.

BRANCHING if-else statements

AGENDA Binary Operations CS 3330 Samira Khan

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Binary representation of integer numbers Operations on bits

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Operators And Expressions

Lecture 5-6: Bits, Bytes, and Integers

Bits, Bytes and Integers

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

More Programming Constructs -- Introduction

Types and Expressions. Chapter 3

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Hardware: Logical View

Binary Values. CSE 410 Lecture 02

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Lecture 13 Bit Operations

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

PHPoC vs PHP > Overview. Overview

Why embedded systems?

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

Fundamental of Programming (C)

CS 24: INTRODUCTION TO. Spring 2015 Lecture 2 COMPUTING SYSTEMS

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

Course Topics - Outline

Computers Programming Course 7. Iulian Năstac

Transcription:

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

C Numbers and Logic Logic deals with two values: True and False Numbers have many values In C, zero is interpreted as logically False Expressions which evaluate to False have a value of 0 In C, every number OTHER than zero is interpreted as 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 2

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

Unary Operator Logical Expression 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: <, <=, ==, >, >=,!= Pitfall: 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

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 6

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

Example short circuit if ((x!= 0) && (37/x >3)) printf( x works\n ); else { printf( x is <=0 or too big to satisfy 37/x>3\n ); printf( Aborting ); exit(1); }

Ternary operator expression 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; 9

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

BITWISE Operators 11

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

Logical vs. Bitwise in Software Logical char x=0b00011111; char y=0b11110011; char z = x && y; x!=0, so x is true y!=0, so y is true z = true && true z = true = 0b0000 00001 Bitwise char x=0b00011111; char y=0b11110011; char w = x & y; x= 0b0001 1111 = 31 y= 0b1111 0011 = -13 &--------------------- w= 0b0001 0011 = 19 13

Logical vs. Bitwise Comparison 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 & ^ 14

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. (used to turn OFF bits) 0 bit forces 0 result 1 bit use other operand s value - or operator 1 if either bit is 1. (used to turn ON bits) 0 bit use other operand s value 1 bit forces 1 result ^ - exclusive or operator 1 if bits are different (selective invert) 0 bit use other operand s value 1 bit use inverse of other operand s value 15

Bit Shifting Expressions Shift Left: << char x=53; char y=x<<1; 0 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 0000. Shift Right: >> char x=53; 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 x <<=1; // multiply by 2 x >>=3; // Divide by 8 (almost) 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 17

Difference between shift right and divide char x = -15; // 0b 1111 0001 char y = x/8; // -15/8 = -1.825 = -1 = 0b 1111 1111 char z = x>>3; // 0b 1111 1000 -> // 0b 1111 1100 -> // 0b 1111 1110 = -2 Integer division always rounds towards zero Rounds down for positive numbers Rounds up for negative numbers Shift right always truncates Always rounds down for both positive and negative numbers

Using Bit Twiddling for Multiple Flags #define ISFASTER 0b00000001; #define ISBETTER 0b00000010; #define ISSTRONGER 0b00000100; #define TOGGLE 0b00001000; char flags=0; // Init all flags off if (my.speed > your.speed) flags = ISFASTER; // turn on faster flag if (your.press > my.press) flags &= ~ISSTRONGER; // turn off stronger flag if (flags & ISBETTER) { // if better flag is on. flags ^=TOGGLE; // Flip the toggle flag

Demo Get an integer number from the user. Write out the binary representation of that number. Write out the hexadecimal representation of that number.

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