commit
bb3e3d5057
10 changed files with 143 additions and 0 deletions
Binary file not shown.
Binary file not shown.
@ -0,0 +1 @@ |
|||||
|
test |
@ -0,0 +1,5 @@ |
|||||
|
default: bfilter.c bfilter.h |
||||
|
$(CC) -g -DTOK_LIB -Wall -Wextra -std=gnu99 -Wpointer-arith -Wmissing-prototypes -lmaa -lm -L. -O3 ./bfilter.c -o test -Wl,-rpath,/home/wes/bfilter; |
||||
|
|
||||
|
unsafe: bfilter.c bfilter.h |
||||
|
$(CC) -DNDEBUG -DTOK_LIB -Wall -std=gnu99 -Wextra -Wpointer-arith -Wmissing-prototypes -lmaa -lm -L. -O3 ./bfilter.c -o bfilter -Wl,-rpath,/home/wes/bfilter; |
@ -0,0 +1,77 @@ |
|||||
|
#include <math.h> |
||||
|
#include <stdint.h> |
||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <ctype.h> |
||||
|
#include <string.h> |
||||
|
#include <assert.h> |
||||
|
#include "error.h" |
||||
|
#include "maa.h" |
||||
|
#include "bfilter.h" |
||||
|
|
||||
|
int countbits(int n) { |
||||
|
int c = 0; |
||||
|
while (n >>= 1) { |
||||
|
c++; |
||||
|
} |
||||
|
return c+1; |
||||
|
} |
||||
|
|
||||
|
int |
||||
|
printbits(int n) { |
||||
|
int c = countbits(n); |
||||
|
int i = c; |
||||
|
int *bits = malloc((sizeof (int)) * c); |
||||
|
|
||||
|
while (n >= 2) { |
||||
|
bits[i-1] = n & 1; |
||||
|
i--; |
||||
|
n >>= 1; |
||||
|
} |
||||
|
bits[i-1] = n & 1; |
||||
|
for (int i = 0; i < c; i++) { |
||||
|
printf("%d", bits[i]); |
||||
|
} |
||||
|
printf("\n"); |
||||
|
free(bits); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
int* |
||||
|
new_bitarray(int size) { |
||||
|
int *barray = malloc((sizeof (int)) * size); |
||||
|
int i; |
||||
|
for(i = 0; i < size; i++) { |
||||
|
barray[i] = 0; |
||||
|
} |
||||
|
return barray; |
||||
|
} |
||||
|
|
||||
|
int |
||||
|
setbit(int* arr, int k) { |
||||
|
/* The position in the array of the int we're looking at */ |
||||
|
int i = k/32; |
||||
|
|
||||
|
/* The position of the bit in the int itself */ |
||||
|
int pos = k % 32; |
||||
|
|
||||
|
unsigned int flag = 1; |
||||
|
|
||||
|
/* Shift the flag to the position of the bit we want to set */ |
||||
|
flag = flag << pos; |
||||
|
|
||||
|
arr[i] = arr[i] | flag; |
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
int |
||||
|
main (void) { |
||||
|
int *test = new_bitarray(5); |
||||
|
setbit(test, 6); |
||||
|
setbit(test, 4); |
||||
|
setbit(test, 2); |
||||
|
printbits(test[0]); |
||||
|
return EXIT_SUCCESS; |
||||
|
} |
||||
|
|
@ -0,0 +1,14 @@ |
|||||
|
typedef |
||||
|
struct { |
||||
|
size_t number; |
||||
|
int *arr; |
||||
|
} |
||||
|
bit_array_t; |
||||
|
|
||||
|
int countbits(int); |
||||
|
|
||||
|
int printbits(int); |
||||
|
|
||||
|
int *new_bitarray(int); |
||||
|
|
||||
|
int setbit(int*, int); |
@ -0,0 +1 @@ |
|||||
|
#define CHECK(ptr) if ((ptr) == NULL) { printf("Failed to allocate memory\n"); exit(EXIT_FAILURE); } |
@ -0,0 +1,21 @@ |
|||||
|
#! /usr/bin/env python2 |
||||
|
|
||||
|
def printbits(n): |
||||
|
if n < 2: |
||||
|
return str(n & 1) |
||||
|
rest = n >> 1 |
||||
|
d = n & 1 |
||||
|
return printbits(rest) + str(d) |
||||
|
|
||||
|
def modifybit(n): |
||||
|
return 1 << n |
||||
|
|
||||
|
test = 0b10010101111 |
||||
|
|
||||
|
print bin(test) |
||||
|
print bin(test | modifybit(3)) |
||||
|
|
||||
|
# bin((1 << 6) - 2) |
||||
|
|
||||
|
#print bin(1292002343) |
||||
|
#print printbits(1292002343) |
@ -0,0 +1,23 @@ |
|||||
|
Two roads diverged in a yellow wood, |
||||
|
And sorry I could not travel both |
||||
|
And be one traveler, long I stood |
||||
|
And looked down one as far as I could |
||||
|
To where it bent in the undergrowth; |
||||
|
|
||||
|
Then took the other, as just as fair, |
||||
|
And having perhaps the better claim, |
||||
|
Because it was grassy and wanted wear; |
||||
|
Though as for that the passing there |
||||
|
Had worn them really about the same, |
||||
|
|
||||
|
And both that morning equally lay |
||||
|
In leaves no step had trodden black. |
||||
|
Oh, I kept the first for another day! |
||||
|
Yet knowing how way leads on to way, |
||||
|
I doubted if I should ever come back. |
||||
|
|
||||
|
I shall be telling this with a sigh |
||||
|
Somewhere ages and ages hence: |
||||
|
Two roads diverged in a wood, and I— |
||||
|
I took the one less traveled by, |
||||
|
And that has made all the difference. |
Loading…
Reference in new issue