bit-ops/bit-ops.c
2020-10-31 20:05:30 +01:00

94 lines
1.9 KiB
C

#include <stdio.h>
void zeroth_bit_test();
void do_arbitrary_stuff();
unsigned char PTAD;
void do_bit_ops() {
PTAD = 0x10;
printf("PTAD = %#x\n* OR\n", PTAD);
PTAD |= 0x01;
printf("PTAD |= 0x01 --> %#x\n", PTAD);
printf("----------\n* AND\n");
printf("PTAD = %#x\n", PTAD=0x1f);
PTAD &= 0xfe;
printf("PTAD &= 0xfe --> %#x\n", PTAD);
printf("----------\n* NOT\n");
printf("PTAD = %#x\n", PTAD=0x10);
PTAD = ~PTAD;
printf("PTAD = ~PTAD --> %#x\n", PTAD);
printf("----------\n* XOR\n");
printf("PTAD = %#x\n", PTAD=0x10);
PTAD ^= 0xfe;
printf("PTAD ^= 0xfe --> %#x\n", PTAD);
zeroth_bit_test();
do_arbitrary_stuff();
}
void zeroth_bit_test() {
PTAD = 0x10;
printf("----------\n* 0th bit test\n");
for (PTAD = 0x00; PTAD < 0x0a; PTAD++){
printf("PTAD = %u (", PTAD);
for (int i = 0; i < 8; i++) {
printf("%d", !!((PTAD << i) & 0x80));
}
printf(")\n");
if ( PTAD & 0x01 ) {
printf("0th bit is 1\n");
} else {
printf("0th bit is 0\n");
}
}
}
void gimme_bin_int(char byte) {
for (int i = 0; i < 8; i++) {
printf("%d", !!((byte << i) & 0x80));
}
printf(")\n");
}
void do_arbitrary_stuff() {
unsigned char PTCD;
/* set 1st bit to 1 */
printf("----------\n* set 1st bit to 1\n");
printf("PTCD = %#x\n", PTCD=0x00);
PTCD |= 0x01;
printf("PTCD |= 0x01 --> %#x (", PTCD);
gimme_bin_int(PTCD);
printf("----------\n* set 2nd bit to 1\n");
printf("PTCD = %u (", PTCD=0b101);
gimme_bin_int(PTCD);
PTCD |= 0b111;
printf("PTCD |= 7 --> %#x (", PTCD);
gimme_bin_int(PTCD);
printf("----------\n* set {6,7}th bit to 1\n");
printf("PTCD = %u (", PTCD=0x3a);
gimme_bin_int(PTCD);
PTCD |= 0xc0;
printf("PTCD |= 1110 0000 --> %#x (", PTCD);
gimme_bin_int(PTCD);
printf("----------\n* set 0th bit to 0\n");
printf("PTCD = %u (", PTCD=0x5d);
gimme_bin_int(PTCD);
PTCD |= 0x50;
printf("PTCD |= 0x00 --> %#x (", PTCD);
gimme_bin_int(PTCD);
}
int main() {
printf("*** bit-ops pls ***\n");
do_bit_ops();
return 0;
}