Skip to content

Instantly share code, notes, and snippets.

@DocBohn
DocBohn / examine_bits.c
Created June 4, 2024 21:11
Demonstration of the use of bitmasks to examine specific bits.
void examine_bits(int16_t x) {
if (x & 0x1)
printf("%d is odd.\n", x);
else
printf("%d is even.\n", x);
if (!x)
printf("%d is zero.\n", x);
else if (x & 0x8000)
printf("%d is negative.\n", x);
else
@DocBohn
DocBohn / iterate_over_alphabet.c
Last active June 17, 2024 23:47
Demonstration of using ASCII characters like integers.
void iterate_over_alphabet(void) {
printf("Iterating over the lower-case letters...\n");
for (char c = 'a'; c <= 'z'; c++)
printf("%c ", c);
printf("\n");
}
@DocBohn
DocBohn / nonterminating_loop.c
Last active June 17, 2024 23:46
Demonstration of the inability to represent 0.1 in binary.
#include <stdio.h>
#include <unistd.h>
#define MS_PER_uS 1000
void nonterminating_loop(void) {
float x;
int duration = 100 * MS_PER_uS;
for (x = 0.0; x != 10.0; x += 0.1) {
printf("x = %f\n", x);
@DocBohn
DocBohn / terminating_loop.c
Last active June 18, 2024 13:33
Demonstration of IEEE 754's decimal floating point type.
#include <stdio.h>
#include <unistd.h>
#define MS_PER_uS 1000
void terminating_loop(void) {
_Decimal32 x;
int duration = 100 * MS_PER_uS;
for (x = 0.0df; x != 10.0df; x += 0.1df) {
printf("x = %Ha\n", x);
@DocBohn
DocBohn / mov_demo.c
Last active June 25, 2024 15:27
Demonstration of copying data from a register or immediate value into a register
long mov_demo1(long i) {
return i;
}
int mov_demo2() {
int i = 3;
return i;
}
@DocBohn
DocBohn / load_store_demo.c
Last active June 25, 2024 02:45
Demonstration of copying data between memory and registers
long load_demo(long *i) {
return *i;
}
long *store_demo() {
long *i; // uninitialized pointer; will probably result in a segmentation fault
*i = 3;
return i;
}
@DocBohn
DocBohn / scalar_demo.c
Last active June 25, 2024 15:33
Demonstration of copying data from one memory location to another
long *scalar_demo(long *i) {
long *j;
*j = *i;
return j;
}
@DocBohn
DocBohn / struct_demo.c
Last active June 25, 2024 15:34
Demonstration of copying data from a struct
struct foo {
long i;
long j;
};
long struct_demo(struct foo *bar) {
return bar->j;
}
@DocBohn
DocBohn / array_demo.c
Last active June 25, 2024 15:37
Demonstration of copying a value from an array
long array_demo1(long *i, long j) {
return i[j];
}
long array_demo2(long *i) {
return i[3];
}
@DocBohn
DocBohn / arithmetic_demo.c
Last active June 25, 2024 16:04
Demonstration of arithmetic
long add_demo(long *i, long j) {
return *i + j;
}
long sub_demo(long *i, long j) {
return *i - j;
}
long mul_demo(long *i, long j) {
return *i * j;