Skip to content

Instantly share code, notes, and snippets.

View cacharle's full-sized avatar

Charles Cabergs cacharle

View GitHub Profile
unsigned char rotate_left(unsigned char b)
{
unsigned char first = b & 0x80;
b <<= 1;
b |= first >> 7;
return b;
}
unsigned char rotate_right(unsigned char b)
{
#define PRINT_BYTE(x) (print_bits((x), 8))
#define PRINT_WORD(x) (print_bits((x), 16))
#define PRINT_DWORD(x) (print_bits((x), 32))
#define PRINT_QWORD(x) (print_bits((x), 64))
void print_bits(unsigned long long int n, int s)
{
if (s == 0)
return;
print_bits(n >> 1, s - 1);
@cacharle
cacharle / formated.txt
Created November 17, 2019 22:22
cheatsheet to read files in c
nom=didier age=43,xp=65,hp=100
nom=dupond age=12,xp=34,hp=110
nom=jean age=1,xp=2,hp=110
-- Reverse Polish Notation parser
-- from: http://learnyouahaskell.com/functionally-solving-problems
rpn :: String -> Float
rpn expr = head $ foldl foldFunc [] (words expr)
where foldFunc acc op
| head op `elem` ['1'..'9'] = (read op :: Float) : acc
| otherwise = applyOp op (acc !! 1) (head acc) : drop 2 acc
applyOp op a b = case op of "+" -> a + b
"-" -> a - b
isPrime :: Int -> Bool
isPrime 2 = True
isPrime 3 = True
isPrime x
| x < 2 = False
| x `mod` 2 == 0 || x `mod` 3 == 0 = False
| otherwise = divCheck 5
where divCheck d
| d * d > x = True
| x `mod` d == 0 || x `mod` (d + 2) == 0 = False
#include <stdio.h>
union Color
{
int hexcode;
struct
{
unsigned char b;
unsigned char g;
unsigned char r;
NAME = name
CC = gcc
CCFLAGS = -Wall -Wextra
LDFLAGS =
HEADER = header.h
SRC = main.c
OBJ = $(SRC:.c=.o)
RM = rm -f
@cacharle
cacharle / main.c
Last active September 11, 2023 13:15
SDL boilerplate files for basic application
#include "sdl_boilerplate.h"
int main(void)
{
GState *gstate = graphics_init(400, 400);
graphics_run(gstate);
graphics_quit(gstate);
return 0;
}
@cacharle
cacharle / sdl_boilerplate.c
Created August 27, 2019 15:34
SDL boilerplate files for basic application
#include <stdbool.h>
#include <SDL2/SDL.h>
#define WINDOW_TITLE "Title"
#define WINDOW_X 20
#define WINDOW_Y 20
static void update(GState *state);
static void event_handler(GState *state);
static void destroy_state(GState *state);
@cacharle
cacharle / split.hs
Created August 19, 2019 16:25 — forked from ifukazoo/split.hs
split
split :: Eq a => a -> [a] -> [[a]]
split _ [] = [[]]
split delim str =
let (before, remainder) = span (/= delim) str
in before:case remainder of
[] -> []
x -> split delim $ tail x