bit-ops/bit-ops.c

87 lines
1.8 KiB
C
Raw Normal View History

2020-10-31 12:47:08 +01:00
#include <stdio.h>
2020-10-31 15:40:43 +01:00
void zeroth_bit_test();
2020-10-31 19:47:47 +01:00
void do_arbitrary_stuff();
2020-10-31 15:40:43 +01:00
unsigned char PTAD;
2020-10-31 12:49:18 +01:00
void do_bit_ops() {
2020-10-31 15:40:43 +01:00
PTAD = 0x10;
2020-10-31 14:33:35 +01:00
printf("PTAD = %#x\n* OR\n", PTAD);
2020-10-31 12:49:18 +01:00
PTAD |= 0x01;
2020-10-31 14:33:35 +01:00
printf("PTAD |= 0x01 --> %#010x (%#x)\n", PTAD, PTAD);
printf("----------\n* AND\n");
printf("PTAD = %#x\n", PTAD=0x1f);
2020-10-31 12:49:18 +01:00
PTAD &= 0xfe;
2020-10-31 14:33:35 +01:00
printf("PTAD &= 0xfe --> %#010x (%#x)\n", PTAD, PTAD);
printf("----------\n* NOT\n");
printf("PTAD = %#x\n", PTAD=0x10);
PTAD = ~PTAD;
printf("PTAD = ~PTAD --> %#010x (%#x)\n", PTAD, PTAD);
printf("----------\n* XOR\n");
printf("PTAD = %#x\n", PTAD=0x10);
PTAD ^= 0xfe;
printf("PTAD ^= 0xfe --> %#010x (%#x)\n", PTAD, PTAD);
2020-10-31 15:40:43 +01:00
zeroth_bit_test();
2020-10-31 19:47:47 +01:00
do_arbitrary_stuff();
2020-10-31 15:40:43 +01:00
}
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");
}
}
2020-10-31 12:49:18 +01:00
}
2020-10-31 19:47:47 +01:00
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 --> %#010x (", 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 --> %#010x (", 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 --> %#010x (", PTCD);
gimme_bin_int(PTCD);
}
2020-10-31 12:49:18 +01:00
2020-10-31 12:47:08 +01:00
int main() {
printf("*** bit-ops pls ***\n");
2020-10-31 12:49:18 +01:00
do_bit_ops();
2020-10-31 12:47:08 +01:00
return 0;
}