Created
January 4, 2014 10:58
-
-
Save dermesser/8254208 to your computer and use it in GitHub Desktop.
Small one-bit array library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <stdlib.h> | |
# include <bitarray.h> | |
struct bit_array* bitarray_init(unsigned int bits) | |
{ | |
struct bit_array* new_array = malloc(sizeof(struct bit_array)); | |
new_array->size = (bits % 8 == 0) ? bits / 8 : (bits / 8) + 1; | |
new_array->array = calloc(new_array->size,sizeof(struct bit_element)); | |
return new_array; | |
} | |
unsigned char bitarray_get(struct bit_array* b , unsigned int position) | |
{ | |
if ( position > (b->size * 8) ) | |
return 2; | |
unsigned int array_offset = position / 8; | |
unsigned int bit_offset = position - (array_offset*8); | |
switch (bit_offset) | |
{ | |
case 0 : return b->array[array_offset].a; | |
case 1 : return b->array[array_offset].b; | |
case 2 : return b->array[array_offset].c; | |
case 3 : return b->array[array_offset].d; | |
case 4 : return b->array[array_offset].e; | |
case 5 : return b->array[array_offset].f; | |
case 6 : return b->array[array_offset].g; | |
case 7 : return b->array[array_offset].h; | |
} | |
} | |
unsigned char bitarray_set(struct bit_array* b, unsigned int position, char value) | |
{ | |
if ( position > (b->size * 8) ) | |
return 1; | |
if ( value != 0 ) | |
value = 1; | |
unsigned int array_offset = position / 8; | |
unsigned int bit_offset = position - (array_offset*8); | |
switch (bit_offset) | |
{ | |
case 0 : b->array[array_offset].a = value; | |
case 1 : b->array[array_offset].b = value; | |
case 2 : b->array[array_offset].c = value; | |
case 3 : b->array[array_offset].d = value; | |
case 4 : b->array[array_offset].e = value; | |
case 5 : b->array[array_offset].f = value; | |
case 6 : b->array[array_offset].g = value; | |
case 7 : b->array[array_offset].h = value; | |
} | |
return 0; | |
} | |
void bitarray_free(struct bit_array* b) | |
{ | |
free(b->array); | |
free(b); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ifndef BITARRAY_H | |
# define BITARRAY_H | |
struct bit_element | |
{ | |
unsigned char a:1, b:1, c:1, d:1, e:1, f:1, g:1, h:1; | |
}; | |
struct bit_array | |
{ | |
struct bit_element* array; // 8 or 4 bytes. | |
unsigned int size; // Size in bytes (n_fields <= size * 8) | |
// 4 bytes -> aligned. | |
}; | |
typedef struct bit_array bit_array; | |
/* | |
* returns NULL if an error occurred. | |
*/ | |
struct bit_array* bitarray_init(unsigned int bits); | |
/* | |
* returns 0 or 1 if successful; else 2 is returned. | |
*/ | |
unsigned char bitarray_get(struct bit_array*, unsigned int position); | |
/* | |
* returns non-zero if an error occurred, else 0. | |
*/ | |
unsigned char bitarray_set(struct bit_array*, unsigned int position, char value); | |
/* | |
* returns non-zero if an error occurred, else 0. | |
*/ | |
void bitarray_free(struct bit_array* b); | |
# endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
project(libbitarray) | |
cmake_minimum_required(VERSION 2.8) | |
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} -O3) | |
include_directories(.) | |
set(libsources | |
bitarray.c | |
) | |
set(testsources | |
test.c | |
) | |
add_library(bitarray STATIC ${libsources}) | |
add_executable(testlibbitarray ${testsources}) | |
target_link_libraries(testlibbitarray bitarray) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Project] | |
Name=libbitarray | |
Manager=KDevCMakeManager | |
VersionControl=kdevgit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The MIT License (MIT) | |
Copyright (c) 2014 Lewin Bormann | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <stdio.h> | |
# include <bitarray.h> | |
int main(int argc, char **argv) | |
{ | |
bit_array* b = bitarray_init(18); | |
bitarray_set(b,0,1); | |
bitarray_set(b,1,1); | |
bitarray_set(b,2,1); | |
char a = bitarray_get(b,0); | |
char d = bitarray_get(b,1); | |
char c = bitarray_get(b,2); | |
printf("%i\n",b->size); | |
bitarray_free(b); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment