Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
Senem KUMOVA METİN CS FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Arithmetic & Logic Unit Does the calculations Everything else in the computer is there to service this unit Handles integers May handle floating point.
Expressions and Arithmetic. Assignments Due – Lab 1 Reading – Chapter 3 –
Introduction to C Programming CE Lecture 8 Bitwise Operations.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.
1 Bitwise Operators. 2 Bitwise Operators (integers) Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement"
Introduction to C Language
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to use the bitwise logical operators in programs ❏ To be able to use.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Logic Circuits I.
PYTHON PROGRAMMING Week 14 – Tuesday. STARTER You need to find out the average of a set of exam marks for a class. You don’t know how many marks there.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Windows Programming, C.-S. Shieh, KUAS EC, Chapter 3 Operators and Expressions.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bitwise Operators in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC.
CPSC 233 Tutorial Xin Jan 24, Assignment 1 Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on.
Addition and Substraction
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
Arithmetic Operations
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.
In decimal we are quite familiar with placing a “-” sign in front of a number to denote that it is negative The same is true for binary numbers a computer.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes.
CompSci From bits to bytes to ints  At some level everything is stored as either a zero or a one  A bit is a binary digit a byte is a binary.
Operator Kinds of Operator Precedence of Operator Type Casting.
Department of Software.
From bits to bytes to ints
CSE 220 – C Programming Expressions.
Precedence and Associativity
Number Systems and Bitwise Operation
Java operatoriai
Lecture 5 from (Chapter 4, pages 73 to 96)
Introduction to Programming
Operators Lecture 10 Fri, Feb 8, 2008.
More about Numerical Computation
Chapter 14 Bitwise Operators Objectives
Bits and Bytes Boolean algebra Expressing in C
Introduction to Programming
Bitwise Operators CS163 Fall 2018.
Homework Homework Continue Reading K&R Chapter 2 Questions?
Chapter 4: Expression and Operator
Bitwise operators.
Bitwise Operators.
Bit Manipulations CS212.
Presentation transcript:

Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types

Senem Kumova Metin Bitwise Operators and Expressions C bitwise operators: logical operators(unary) –bitwise complement ~ –bitwise and & –bitwise exclusive or ^ –bitwise (inclusive) or | shift operators left shift << right shift >>

Senem Kumova Metin

Bitwise Complement Two’s Complement int n= 7; Binary representation of n : Bitwise complement of n : Two’s complement representation of –n : Value of –n : -7

Senem Kumova Metin SHIFT OPERATORS : LEFT SHIFT char c = ‘Z’; // c<<n c : shift and feed with n 0’s c<<1 : c<<4 :

Senem Kumova Metin SHIFT OPERATORS : RIGHT SHIFT for unsigned types shift 0’s for signed types shift 1’s int a : a>> unsigned int b : b>>

Senem Kumova Metin MASKS particular values used typically with the & operator to extract a given series of bits EXAMPLE: Message to be sent : 542 ( ) Encyrpted(coded) message : X101 1X00 X010 (X can be a 0 or 1) Mask (decoder) in receiver : Mask & coded message :

Senem Kumova Metin Enumeration Types EXAMPLE : main() {… enum day {su, mo, tu, we, th, fr, sa}; /* enumeration type declaration */ enum day d1=su, d2; /* declaration/initialization of variables of type enum day */ d2=find_next_day(d1); …. } enum day find_next_day(enum day d) { enum day next_day; switch (d) {case su: next_day = mo; break; case mo: next_day = tu; break;... case sa: next_day = su; break; } return next_day; }

Senem Kumova Metin Enumeration Types EXAMPLE : main() {… enum day {su=1, mo, tu, we, th, fr, sa}; //INITIALIZATION enum day d1=su, d2; d2=find_next_day(d1); …. } enum day find_next_day(enum day d) { enum day next_day; switch (d) {case su: next_day = mo; break; case mo: next_day = tu; break;... case sa: next_day = su; break; } return next_day;}

Senem Kumova Metin CHAPTER 8 The Preprocessor

Senem Kumova Metin The use of #include EXAMPLES : #include #include”my_header.h”

Senem Kumova Metin The use of #define EXAMPLES : #define PI 3.14 #define EOF (-1) #define SIZE 250 #define SECONDS_PER_DAY (60*60*24) #define EQ == // while (1 EQ 2)

Senem Kumova Metin The use of #define: Macros with arguments EXAMPLE 1: #define SQ(x) ((x)*(x)) int i =5; int x=0; x=SQ(3+i)  ((3+i)*(3+i))  x=? EXAMPLE 2: #define SQ(x) (x*x) int i =5; int x=0; x=SQ(3+i)  (3+i*3+i)  x=?

Senem Kumova Metin The use of #define: Macros with arguments #define SQ(x) ((x)*(x)) #define CUBE(x) (SQ(x)*(x)) #undef SQ // undefines the macro #undef CUBE

Senem Kumova Metin The Type Definitions and Macros in stddef.h An identifier can be associated with a specific type with typedef facility EXAMPLE : typedef char uppercase; uppercase c, u[100];

Senem Kumova Metin The macros in stdio.h & ctype.h ctype.h  includes macros and function prototypes for character tests and converts them SOME EXAMPLES (pg.383) char x; isupper(x)  nonzero is returned if x is an uppercase letter isalpha(x)  nonzero is returned if x is a letter isdigit(x)  nonzero is returned if x is a digit toupper(x)  changes x to uppercase tolower(x)  changes x to lowercase

Senem Kumova Metin Conditional Compilation The preprocessor has directives for conditional compilation used to aid in program testing and for writing code that it is more easily portable from one machine to another. Lines beginning with #if, #ifdef, #ifndef, #elif, #else, and #endif are used for this Each preprocessing directive of the form given in examples provides for conditional compilation of the code that follows until the preprocessing directive #endif is reached

Senem Kumova Metin CONDITIONAL COMPILATION EXAMPLES: #if constant_integral_expression... /* code will be compiled if constant_integral_expression != 0 */ #endif #ifdef identifier /* or #if defined identifier */... /* identifier must be defined by #define before */ #endif #ifndef identifier... /* idenitifer must be currently undefined */ #endif

Senem Kumova Metin CONDITIONAL COMPILATION Each of the #if directive can be followed by directive similar to if-else C statement –#elif constant_integral_expression /* elif == "else-if" */ –#else –#endif The defined operator can be used with preprocessing directives, e.g., #if defined(HP3000) || defined(SUN3) && !defined(SUN4).... /* machine-dependent code */ #endif