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