CSE 230 Intermediate Programming in C and C++

Similar documents
Bitwise Operators. Fall Jinkyu Jeong GEBD029: Basis and Practice in Programming Fall 2014 Jinkyu Jeong

CSE 230 Intermediate Programming in C and C++

Computer Science 5C Chapter 7--Enumeration Types and Typedef

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

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

Arithmetic Operators. Portability: Printing Numbers

CSE 230 Intermediate Programming in C and C++ Functions

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

Expressions and Precedence. Last updated 12/10/18

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Operators in java Operator operands.

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

JAVA OPERATORS GENERAL

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

OBJECT ORIENTED PROGRAMMING USING C++

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

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Programming in C++ 5. Integral data types

Fall, 2015 Prof. Jungkeun Park

Engineering Computing I

Infix to Postfix Conversion

Computers Programming Course 6. Iulian Năstac

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

CSE 214 Computer Science II Java Classes and Information Hiding

SECTION II: LANGUAGE BASICS

Unit-2 (Operators) ANAND KR.SRIVASTAVA

CS2141 Software Development using C/C++ C++ Basics

OBJECT ORIENTED PROGRAMMING

Enumerated Types. Mr. Dave Clausen La Cañada High School

Types, Operators and Expressions

Informatics Ingeniería en Electrónica y Automática Industrial

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Operators in C. Staff Incharge: S.Sasirekha

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Review of the C Programming Language for Principles of Operating Systems

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

Lecture 02 C FUNDAMENTALS

CSE 230 Intermediate Programming in C and C++ Recursion

Applied Computer Programming

DEPARTMENT OF MATHS, MJ COLLEGE

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

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

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

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

Fundamentals of Programming Session 20

Operators and Expressions:

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

The Arithmetic Operators

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

Part I Part 1 Expressions

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

DigiPen Institute of Technology

CS 241 Data Organization Binary

CSE 230 Intermediate Programming in C and C++

Types, Operators and Expressions

Java enum, casts, and others (Select portions of Chapters 4 & 5)

12 CREATING NEW TYPES

Computer Organization & Systems Exam I Example Questions

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Information Science 1

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

File Handling in C. EECS 2031 Fall October 27, 2014

CSE I-Sem)

Binary representation of integer numbers Operations on bits

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

Chapter 3: Operators, Expressions and Type Conversion

CS 253. January 14, 2017

Review of the C Programming Language

Structures and Pointers

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Computers Programming Course 5. Iulian Năstac

CSE 214 Computer Science II Stack

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

C Programming Language (Chapter 2 of K&R) Variables and Constants

EECS402 Lecture 24. Something New (Sort Of) Intro To Function Pointers

Expression and Operator

.Net Technologies. Components of.net Framework

LAB A Translating Data to Binary

C++/Java. C++: Hello World. Java : Hello World. Expression Statement. Compound Statement. Declaration Statement

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

UNIT- 3 Introduction to C++

Fundamental of Programming (C)

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

CSE 230 Computer Science II (Data Structure) Introduction

UNIT 3 OPERATORS. [Marks- 12]

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CSE 130 Introduction to Programming in C Control Flow Revisited

Fundamentals of Programming

Operators & Expressions

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Transcription:

CSE 230 Intermediate Programming in C and C++ Bitwise Operators and Enumeration Types Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/

Overview Bitwise Operators The bitwise operators act on integral expressions represented as binary digits. Expressions with bitwise operators are explicitly system-dependent Useful in packing and unpacking data Enumeration Types User defined types Allow the programmer to name a finite set together with its elements, which are called enumerators Shebuti Rayana (CS, Stony Brook University) 2

Bitwise Operators Logical Operators Types of Bitwise Operators (unary) bitwise complement Bitwise AND Bitwise inclusive OR Bitwise exclusive OR Shift Operators Left shift << ~ & ^ Right shift >> Shebuti Rayana (CS, Stony Brook University) 3

Precedence and Associativity Operators Associativity () [] ++(postfix) --(postfix) Left to right ++ -- (prefix)! ~ sizeof() + - (unary) &(address) *(pointer) Right to left * / % Left to right + - Left to right << >> Left to right < <= > >= Left to right ==!= Left to right & ^ Left to right Left to right Left to right && Left to right Left to right?: Right to left = += -= *= /= %= <<= >>= &= ^= = Right to left, (comma) Left to right Shebuti Rayana(CS, Stony Brook University) 4

Bitwise Complement ~ is called one s complement Inverts all the bits, (0 s become 1 s and 1 s become 0 s) Example: int a = 70707; in binary 00000000 00000001 00010100 00110011 ~a is one s complement for a 11111111 11111110 11101011 11001100 So ~a becomes -70708 Shebuti Rayana (CS, Stony Brook University) 5

Value of n Two s Complement The two's complement representation of a nonnegative integer n is the bit string obtained by writing n in base 2. If we take the bitwise complement of the bit string and add 1 to it, we obtain the two's complement representation of -n Binary Representation Bitwise Complement Two s Complement Representation of -n 7 00000000 00000111 11111111 11111000 11111111 11111001-7 8 00000000 00001000 11111111 11110111 11111111 11111000-8 9 00000000 00001001 11111111 11110110 11111111 11110111-9 -7 11111111 11111001 00000000 00000110 00000000 00000111 7 Value of -n *Two lower order bytes in 4 bytes machine *A machine which uses this representation is called a two s complement machine Shebuti Rayana (CS, Stony Brook University) 6

Two s Complement (cont.) O : all bits off, -1: all bits on if a binary string is added to its bitwise complement the result has all bits on, which is the two's complement representation of -1. Negative numbers are characterized by having the high bit on. On a two's complement machine, the hardware that does addition and bitwise complementation can be used to implement subtraction. The operation a - b is the same as a + (-b), and -b is obtained by taking the bitwise complement of b and adding 1. Shebuti Rayana (CS, Stony Brook University) 7

Bitwise Binary Logical Operators Single bit Operations a b a&b a^b a b 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 0 1 *Operated on bit position by bit position Shebuti Rayana (CS, Stony Brook University) 8

Examples: Bitwise Operators Declaration and Initialization int a = 33333; int b = -77777; Expression Representation Value a 00000000 00000000 10000010 00110101 33333 b 11111111 11111110 11010000 00101111-77777 a&b 00000000 00000000 10000000 00100101 32805 a^b 11111111 11111110 01010010 00011010-110054 a b 11111111 11111110 11010010 00111111-77249 ~(a b) 00000000 00000001 00101101 11000000 77248 (~a&~b) 00000000 00000001 00101101 11000000 77248 De Morgan s Law: ~(a b) = (~a&~b), ~(a&b) = (~a ~b) Shebuti Rayana (CS, Stony Brook University) 9

Left Shift Operator The two operands of a left shift operator must be integral expressions. Example: expr1 << expr2, the bit representation of expr1 is shifted to the left by expr2 positions. On the low-order end, O s are shifted in. Both the operands are promoted to integral types before shifting The resulting type is the type of left operand Shebuti Rayana (CS, Stony Brook University) 10

Example: Left shift Declaration and Initialization Char c = Z ; Expression Representation Action c 00000000 00000000 00000000 01011010 unshifted c << 1 00000000 00000000 00000000 10110100 Left shifted 1 c << 4 00000000 00000000 00000101 10100000 Left shifted 4 c << 31 00000000 00000000 00000000 00000000 Left shifted 31 Shebuti Rayana (CS, Stony Brook University) 11

Right Shift Operator The right shift operator is not similar to the left shift operator For unsigned expressions shifted positions are filled with 0 s But for signed expressions: (i) some machines shift in 0 s, and (ii) some shift in the sign bit (left most bit or high order bit) Sign bit is 0 for nonnegative integers and 1 for negative integers Shebuti Rayana (CS, Stony Brook University) 12

Example: Right Shift Declaration and Initialization int a = 1 << 31; // shift 1 to the high bit unsigned b = 1 << 31; Expression Representation Action a 10000000 00000000 00000000 00000000 unshifted a >> 3 11110000 00000000 00000000 00000000 Right shifted 3 b 10000000 00000000 00000000 00000000 unshifted b >> 3 00010000 00000000 00000000 00000000 Right shifted 3 If the right operand of a shift operator is negative or has a value that equals or exceeds the number of bits used to represent the left operand, then the behavior is undefined. Shebuti Rayana (CS, Stony Brook University) 13

Precedence and Associativity Declaration and Assignments unsigned a = 1, b = 2; Expression Equivalent Expression Representation a << b >> 1 (a << b) >> 1 00000000 00000010 2 a << 1 + 2 << 3 (a << (1 + 2) ) << 3 a+b << 12 * a >> b ((a+b) << (12 * a)) >> b Value 00000000 01000000 64 00001100 00000000 3072 *two low order bytes are shown only *in C++, the two shift operators are overloaded and used for input/output. Overload -ing in C++ is a method of giving existing operators and functions additional meanings. Shebuti Rayana (CS, Stony Brook University) 14

Masks A mask is a constant or variable, that is used to extract desired bits from another variable or expression. if we wish to find the value of a particular bit in an expression, we can use a mask that is 1 in that position and 0 elsewhere. Example: int i, mask = 1; for(i=0; i<10; i++); printf( %d, i & mask); 00000000 00000000 00000000 00000001 This code prints the right most bit of every number in the range [0,9] Shebuti Rayana (CS, Stony Brook University) 15

More Example: Mask 1 << 2, can be used as a mask for third bit (v & (1 << 2))? 1 : 0 Another mask is 255 = 2 8 1, 00000000 00000000 00000000 11111111 v & 255 will give only the low order byte, as such, 255 is called mask for low-order byte Shebuti Rayana (CS, Stony Brook University) 16

Printing an Integer Bitwise #include <limits.h> void bit_print(int a){ int i; int n = sizeof(int) * CHAR_BIT; int mask = 1 << (n - 1); // mask 100 0 } for(i=1; i < n; i++){ putchar(((a & mask) == 0)? 0 : 1 ); a <<= 1; if(i % CHARBIT == 0 && i < n) putchar( ); } Shebuti Rayana (CS, Stony Brook University) 17

Packing Bitwise expressions help in data compression Saving both time and space Example: pack 4 char into an int #include <limits.h> int pack(char a, char b, char c, char d){ } int p = a; p = (p << CHAR_BIT) b; p = (p << CHAR_BIT) c; p = (p << CHAR_BIT) d; return p; Shebuti Rayana (CS, Stony Brook University) 18

Packing (cont.) printf("abcd == "); bit_print(pack('a', 'b', 'c', 'd')); putchar(' \n'); Output: 97 98 abed = 01100001 01100010 01100011 01100100 99 100 Shebuti Rayana (CS, Stony Brook University) 19

Unpacking #include <limits.h> int unpack(int p, int k){ //k=0,1,2,3 int n = k*char_bit; //n=0,8,16,24 unsigned mask = 255; mask <= n; return ((p & mask) >> n); } Shebuti Rayana (CS, Stony Brook University) 20

Unpacking (cont.) Expression Binary Representation Value p 11111111 11001001 01100000 10010111-3579753 mask 00000000 11111111 00000000 00000000 16711680 p & mask 00000000 11001001 00000000 00000000 13172736 (p & mask) >> n 00000000 00000000 00000000 11001001 201 Shebuti Rayana (CS, Stony Brook University) 21

Enumeration Types User defined types Provides a means of naming a finite set, and declaring identifiers as elements of the set. Keyword: enum Example: enum day {sun, mon, tue, wed, thu, fri, sat} day is a user defined enumeration type The identifiers sun,, sat are constants of type int By default, the first one is 0, and each succeeding one has the next integer value. Shebuti Rayana (CS, Stony Brook University) 22

Enumeration Types (cont.) This declaration is an example of a type specifier, which we also think of as a template. Declaration of a variable of type enum: enum day d1, d2; d1 and d2 can only take values from the set day Initialization: d1 = fri; Condition check: if(d1 == d2){/*do something*/} enum day is a type, enum by itself is not a type Shebuti Rayana (CS, Stony Brook University) 23

Enumeration Types (cont.) The enumerators can be initialized Variables can be declared along with the template enum suit {clubs = 1, diamonds, hearts, spades} a, b, c; As clubs is initialized to 1, diamonds, hearts, and spades have the values 2,3, and 4, respectively. enum fruit {apple = 7, pear, orange = 3, lemon} frt; As apple is initialized to 7, pear has value 8. Similarly, because orange has value 3, lemon has value 4. Valid types: enum veg {beet = 17, carrot = 17, corn = 17} vege1, vege2; enum {fir, pine} tree; Shebuti Rayana (CS, Stony Brook University) 24

Example: enum /* compute the next day */ enum day {sun, mon, tue, wed, thu, fri, sat} typedef enum day day; day find_next_day(day d){ } if((int) d >= 0 && (int) d < 7) return ((day)(((int)d+1))); Shebuti Rayana (CS, Stony Brook University) 25