Download presentation
Presentation is loading. Please wait.
Published byTate Walston Modified over 9 years ago
1
Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types
2
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 >>
3
Senem Kumova Metin
4
Bitwise Complement Two’s Complement int n= 7; Binary representation of n : 00000000 00000111 Bitwise complement of n : 11111111 11111000 Two’s complement representation of –n : 11111111 11111001 Value of –n : -7
5
Senem Kumova Metin SHIFT OPERATORS : LEFT SHIFT char c = ‘Z’; // c<<n c : 00000000 00000000 00000000 01011010 shift and feed with n 0’s c<<1 : 00000000 00000000 00000000 10110100 c<<4 : 00000000 00000000 00000101 10100000
6
Senem Kumova Metin SHIFT OPERATORS : RIGHT SHIFT for unsigned types shift 0’s for signed types shift 1’s int a : 10000000 00000000 00000000 00000000 a>>2 11100000 00000000 00000000 00000000 unsigned int b : 10000000 00000000 00000000 00000000 b>>2 00100000 00000000 00000000 00000000
7
Senem Kumova Metin MASKS particular values used typically with the & operator to extract a given series of bits EXAMPLE: Message to be sent : 542 (101 100 010) Encyrpted(coded) message : X101 1X00 X010 (X can be a 0 or 1) Mask (decoder) in receiver : 0111 1011 0111 Mask & coded message : 0101 1000 0010
8
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; }
9
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;}
10
Senem Kumova Metin CHAPTER 8 The Preprocessor
11
Senem Kumova Metin The use of #include EXAMPLES : #include #include”my_header.h”
12
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)
13
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=?
14
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
15
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];
16
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
17
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
18
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
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.