






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Bitwise Operators and Macros, Applications of Bitwise operators, Macros, Macro Arguments, Typecasting, Types of Typecasting, Assertions, Error checking, Switch and case keywords. As you can see in this file, how descriptive above mentioned points are explained in this lecture of computer programming. VU is one of best university for computer science in our country.
Typology: Study notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







- Chapter An operator that manipulates individual bits is called a bitwise operator. The most familiar operators are the addition operator (+) etc and these operators work with bytes or groups of bytes. Occasionally, however, programmers need to manipulate the bits within a byte.
C++ provides operators to work with the individual bits in integers. For this to be useful, we must have some idea of how integers are represented in binary. For example the decimal number 3 is represented as 11 in binary and the decimal number 5 is represented as 101 in binary.
Purpose Operator example complement ~i and i&j exclusive or i^j inclusive or i|j shift left i<
Bitwise AND operator
0 AND 0 = 0 0 AND 1 = 0 1 AND 0 = 0 1 AND 1 = 1
Bitwise OR operator
0 OR 0 = 0 0 OR 1 = 1 1 OR 0 = 1 1 OR 1 = 1
Bitwise OR operator
}//end loop }
Here are some modifications that could be made to this code.
A typedef declaration lets you define your own identifiers that can be used in place of type specifiers such as int , float , and double. The names you define using typedef are NOT new data types. They are synonyms for the data types or combinations of data types they represent.
A typedef declaration does not reserve storage. When an object is defined using a typedef identifier, the properties of the defined object are exactly the same as if the object were defined by explicitly listing the data type associated with the identifier.
The following statements declare LENGTH as a synonym for int , then use this typedef to declare length, width, and height as integral variables.
typedef int LENGTH;
LENGTH length, width, height;
The following declarations are equivalent to the above declaration:
int length, width, height;
Similarly, you can use typedef to define a struct type. For example:
typedef struct {
int scruples;
int drams;
int grains;
} WEIGHT;
The structure WEIGHT can then be used in the following declarations:
WEIGHT chicken, cow, horse, whale;
The proposed feature is intended to be a natural application of existing template syntax to the existing typedef keyword. Interactions with the rest of the language are limited because typedef templates do not create a new type or extend the type system in any way; they only create synonyms for other types.
A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls.
You may define any valid identifier as a macro, even if it is a C keyword. The preprocessor does not know anything about keywords. This can be useful if you wish to hide a keyword such as const from an older compiler that does not understand it. However, the preprocessor operator defined can never be defined as a macro, and C++'s named operators cannot be macros when you are compiling C++.
Function-like macros can take arguments , just like true functions. To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like. The parameters must be valid C identifiers, separated by commas and optionally whitespace.
To invoke a macro that takes arguments, you write the name of the macro followed by a list of actual arguments in parentheses, separated by commas. The invocation of the macro need not be restricted to a single logical line--it can cross as many lines in the source file as you wish. The number of arguments you give must match the number of parameters in the macro definition. When the macro is expanded, each use of a parameter in its body is replaced by the tokens of the corresponding argument. (You need not use all of the parameters in the macro body.)
As an example, here is a macro that computes the minimum of two numeric values, as it is defined in many C programs, and some uses.
#define min(X, Y) ((X) < (Y)? (X) : (Y)) x = min(a, b); ==> x = ((a) < (b)? (a) : (b)); y = min(1, 2); ==> y = ((1) < (2)? (1) : (2)); z = min(a + 28, p); ==> z = ((a + 28) < (p)? (a + 28) : (*p));
//Note the use of the int version of x to //output a number and the use of (char) to // typecast the x into a character //which outputs the ASCII character that //corresponds to the current number } return 0; }
An assertion statement specifies a condition at some particular point in your program. An assertion specifies that a program satisfies certain conditions at particular points in its execution. There are three types of assertion:
Preconditions
Post conditions
Invariants
An assertion violation indicates a bug in the program. Thus, assertions are an effective means of improving the reliability of programs. In other words, they are a systematic debugging tool.
It is important to distinguish between program errors and run- time errors:
Assertions are not a mechanism for handling run-time errors. For example, an assertion violation caused by the user inadvertently entering a negative number when a positive number is expected is poor program design. Cases like this must be handled by appropriate error-checking and recovery code (such as requesting another input), not by assertions.
Realistically, of course, programs of any reasonable size do have bugs, which appear at run-time. Exactly what conditions are to be checked by assertions and what by run-time error- checking code is a design issue. Assertions are very effective in reusable libraries, for example, since i) the library is small enough for it to be possible to guarantee bug-free operation, and ii) the library routines cannot perform error- handling because they do not know in what environment they will be used. At higher levels of a program, where operation is more complex, run-time error-checking must be designed into the code.
By default, ANSI C compilers generate code to check assertions at run-time. Assertion- checking can be turned off by defining the NDEBUG flag to your compiler, either by inserting #define NDEBUG in a header file such as stdhdr.h, or by calling your compiler with the -dNDEBUG option: cc -dNDEBUG ... This should be done only you are confident that your program is operating correctly, and only if program run-time is a pressing concern.
The switch-case statement is a multi-way decision statement. Unlike the multiple decisions statement that can be created using if-else, the switch statement evaluates the conditional expression and tests it against numerous constant values. The branch corresponding to the value that the expression matches is taken during execution.
The value of the expressions in a switch-case statement must be an (integer, char, short, long), etc. Float and double are not allowed.
The syntax is :
switch( expression ) { case constant-expression1: statements1; [case constant-expression2: statements2;] [case constant-expression3: statements3;] [default : statements4;] }
The case statements and the default statement can occur in any order in the switch statement. The default clause is an optional clause that is matched if none of the constants in the case statements can be matched.
Consider the example shown below:
switch( Grade ) { case 'A' : printf( "Excellent" ); case 'B' : printf( "Good" ); case 'C' : printf( "OK" ); case 'D' : printf( "Mmmmm...." );
char Ch; . . switch( Ch ) { /* Handle lower-case characters */ case 'a' : case 'b' : . . . case 'z' : printf( "%c is a lower-case character.\n", Ch ); printf( "Its upper-case is %c.\n" toupper(Ch) ); break;
/* Handle upper-case characters */ case 'A' : case 'B' : . . . case 'Z' : printf( "%c is a upper-case character.\n", Ch ); printf( "Its lower-case is %c.\n" tolower(Ch) ); break;
/* Handle digits and special characters */
default : printf( "%c is not in the alphabet.\n", Ch ); break; } ..
In this lecture, we have learned about the three major bitwise operators AND, OR, XOR. Bitwise operators operates on individual bits.
Using “typedefs” provide an easy way to avoid the long names during the declarations and thus make our code more simple. We have also discussed about the typecasting. It is making a variable of one type, act like another type for one single application. The two types of type casting includes the implicit type casting and the explicit type casting.
In C, the assertions are implemented with standard assert macro, the argument to assert must be true when the macro is executed, otherwise the programmes aborts and printouts an error message.
The switch-case statement is a multi-way decision statement. Unlike the multiple decisions statement that can be created using if-else, the switch statement evaluates the conditional expression and tests it against numerous constant values.