my notes for c
I enjoy ANSI c.
example c program:
#include <stdio.h>
enum Colors {
RED,
BLUE,
GREEN
};
typedef struct {
int width;
int height;
enum Colors color;
} rect_t;
typedef struct {
double x;
double y;
} point_t;
void
rect_init(rect_t *rect, int width, int height, enum Colors color)
{
if (!rect) return;
rect->width = width;
rect->height = height;
rect->color = color;
}
point_t
rect_get_middle(rect_t *rect)
{
point_t point {
(double)(rect->width)/2.0,
(double)(rect->height)/2.0
};
return point;
}
void
rect_info(rect_t *rect)
{
point_t center_point = rect_get_middle(rect);
printf("width: %d, height: %d, color: %u, center point: (%lf, %lf)\n",
rect->width, rect->height, rect->color, center_point.x, center_point.y);
}
int
main(int argv, char **argc)
{
rect_t rect = {15, 15, BLUE};
rect_info(&rect);
return 0;
}
to compile the code, you need to use a compiler;
c89 -o example example.c
list of some bitwise “tricks” i would like to put here
/* minimum value between two variabls */ int min = y ^ ((x ^ y) & -(x < y)); /* so the maximum would be */ int max = x ^ ((x ^ y) & -(x < y)); /* absolute value */ int mask = x >> (sizeof(int) * 8 - 1); int abs = (x + mask) ^ mask; /* check power of 2 */ typedef unsigned char bool_t; bool_t is_power_of_two = (x>0) && (x&(x-1)==0);