0

I wish to store some binary values in an array in C. How it can be done and how can i access any bit of a binary number.

Plz let me know if i i am unclear in posting the doubt.

Prashant Singh
  • 3,695
  • 12
  • 60
  • 106

1 Answers1

3

Store your values in a normal array and write a simple function that would pick proper element and state of its bit, something like

int get_bit_from_array( unsigned char *A, int element, int bit ) {
    return A[element] &  ( 1 << bit ) ;                 
}

or even

int get_bit_from_array( unsigned char *A, int bit_absolute) {
    int element = bit_absolute / 8;
    int bit = bit_absolute % 8;
    return A[element] &  ( 1 << bit ) ;                 
}
Jakub M.
  • 30,095
  • 43
  • 103
  • 169