Graphics Programs Reference
In-Depth Information
for(i=0; i < 4; i++) {
bit_a = (i & 2) / 2; // Get the second bit.
bit_b = (i & 1); // Get the first bit.
printf("%d | %d = %d\n", bit_a, bit_b, bit_a | bit_b);
}
printf("\nbitwise AND operator &\n");
for(i=0; i < 4; i++) {
bit_a = (i & 2) / 2; // Get the second bit.
bit_b = (i & 1); // Get the first bit.
printf("%d & %d = %d\n", bit_a, bit_b, bit_a & bit_b);
}
}
The results of compiling and executing bitwise.c are as follows.
reader@hacking:~/booksrc $ gcc bitwise.c
reader@hacking:~/booksrc $ ./a.out
bitwise OR operator |
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
bitwise AND operator &
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
reader@hacking:~/booksrc $
The flags used for the open() function have values that correspond to
single bits. This way, flags can be combined using OR logic without destroy-
ing any information. The fcntl_flags.c program and its output explore some
of the flag values defined by fcntl.h and how they combine with each other.
fcntl_flags.c
#include <stdio.h>
#include <fcntl.h>
void display_flags(char *, unsigned int);
void binary_print(unsigned int);
int main(int argc, char *argv[]) {
display_flags("O_RDONLY\t\t", O_RDONLY);
display_flags("O_WRONLY\t\t", O_WRONLY);
display_flags("O_RDWR\t\t\t", O_RDWR);
printf("\n");
display_flags("O_APPEND\t\t", O_APPEND);
display_flags("O_TRUNC\t\t\t", O_TRUNC);
display_flags("O_CREAT\t\t\t", O_CREAT);
Search WWH ::




Custom Search