Lesson 05 Macro and BitManipulation
Lesson 05 Macro and BitManipulation
Macro and
Bit Manipulation
FSA,HoChiMinh
1
1 Macro
Table of
Contents 2 Bitwise Operations
3 Bit Fields
2
Macro
3
Macro
Macro definition
Object-like Macros
Function-like Macros
Stringification and Concatenation
Undefining and Redefining Macros
4
Macro Definition
What is macro
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
Macro is defined using #define preprocessor directive in the C
language.
When to use
When creating constants that represent numbers, strings or expressions.
5
Macro Definition
Macro classification
• Predefined macro
• User-defined macro
6
Object-like macros
#define MACRO_NAME macro’s body
An object-like macro is a simple identifier which will be replaced by a code fragment. It is
called object-like because it looks like a data object in code that uses it. They are most
commonly used to give symbolic names to numeric constants.
Give symbolic names to numeric constants
The macro’s body end at the end of the #define line
Single line macro:
#define SIZE 10
Upper case
Multiple line macro:
#define NUMBERS 1, \
2
7
Function-like macros
#define macro_name(list of parameters) macro’s body
Also define macros whose use looks like a function
Lower call. These are called function-like macros.
case No white
space extern void foo(void);
#define foo() /* optimized inline version */
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
#define f () callback()
x = min(a, b); ==> x = ((a) < (b) ? (a) : (b)); ...
?
y = min(1, 2); ==> y = ((1) < (2) ? (1) : (2)); foo(); ?
funcptr = foo; ?
f() ?
8
Stringification and token pasting
struct command {
char *name;
Token pasting
void (*function) (void);
preprocessing
};
operator
#define COMMAND(NAME) { #NAME, NAME ##
_command }
struct command commands[] = {
COMMAND (quit),
COMMAND (help),
}; Stringification
struct command commands[] = { preprocessing
{ "quit", quit_command }, operator
{ "help", help_command },
};
9
Undefining and Redefining
Macros
#ifdef TRUE
#undef TRUE
#define TRUE 1
#endif
10
BITWISE OPERATION (1)
Symbol Operation
bit a bit b a&b a|b a^b
| bitwise inclusive OR 0 1 0 1 1
1 0 0 1 1
^ bitwise XOR (eXclusive OR)
1 1 1 1 0
<< Left shift
bit a ~a
>> Right shift
0 1
~ bitwise NOT (one's complement)
(unary) 1 0
11
Bitwise
Operations
12
BITWISE OPERATION (2)
Shift operations
13
BITWISE OPERATION (3)
#include <stdio.h>
void main() {
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */ OUTPUT:
printf("Line 1 - Value of c is %d\n", c ); Line 1 - Value of c is 12
c = a | b; /* 61 = 0011 1101 */ Line 2 - Value of c is 61
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
Line 3 - Value of c is 49
printf("Line 3 - Value of c is %d\n", c ); Line 4 - Value of c is -61
c = ~a; /*-61 = 1100 0011 */ Line 5 - Value of c is 240
printf("Line 4 - Value of c is %d\n", c ); Line 6 - Value of c is 15
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
}
14
Bit Fields
15
Bit Fields - 1
This structure requires 8 bytes of memory space but in actual, we are
going to store either 0 or 1 in each of the variables. The C programming
language offers a better way to utilize the memory space in such
situations.
struct {
unsigned int widthValidated;
unsigned int heightValidated;
} status;
16
Bit Fields - 2
struct {
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status;
The above structure requires 4 bytes of memory space for status variable, but only 2 bits will be used
to store the values.
If you will use up to 32 variables each one with a width of 1 bit, then also the status structure will use 4
bytes. However as soon as you have 33 variables, it will allocate the next slot of the memory and it will
start using 8 bytes.
17
Bit Fields - 3
#include <stdio.h>
#include <string.h>
/* define simple structure */
struct {
unsigned int widthValidated;
unsigned int heightValidated;
} status1; OUTPUT:
/* define a structure with bit fields */
struct {
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status2;
int main( ) {
printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
printf( "Memory size occupied by status2 : %d\n", sizeof(status2));
return 0;
}
18
Bit Field Declaration - 1
19
Bit Field Declaration - 2
The following table describes the variable elements of a bit field:
20
Quiz(1)
1. Write macros to define MASK and SHIFT location of each bit field.
21
Quiz(2)
22
Thank you
Q&A
23