論理積(&),論理和(|),排他的論理和(^),否定(~),右シフト,左シフトの演算サンプル
#include <stdio.h>
/* prototype */
void and_oper();
void or_oper();
void exclusive_or_oper();
void not_oper();
void right_shift_oper();
void left_shift_oper();
int main(int argc, char *argv[]) {
and_oper();
or_oper();
exclusive_or_oper();
not_oper();
right_shift_oper();
left_shift_oper();
return 0;
}
void and_oper() {
char a = 0x08;
char b = 0x08;
/*
* 0000 1000
* 0000 1000 &
* -----------
* 0000 1000
*/
printf("%d\n", a & b);
}
void or_oper() {
char a = 0x08;
char b = 0xaa;
/*
* 0000 1000
* 1011 1011 |
* ---------
* 1011 1011
*/
printf("%d\n", a|b);
}
void exclusive_or_oper() {
char a = 0x09;
char b = 0x03;
/**
* 0000 1001
* 0000 0011 ^
* ---------
* 0000 1010
*/
printf("%d\n", a^b);
}
void not_oper() {
// one's complement
char a = 0xcf;
/**
* 1100 1111 ~
* ----------
* 0011 0000
*/
printf("%d\n", a);
}
void right_shift_oper() {
int cnt;
char t = 1;
for (cnt = 0; cnt < 8; cnt++) {
t = t << 1;
printf("%d ", t);
}
printf("\n");
}
void left_shift_oper() {
int cnt;
char t = 0x7f;
for (cnt = 0; cnt < 8; cnt++) {
t = t >> 1;
printf("%d ", t);
}
printf("\n");
}