Reading bits of a character

Hello!
I have stored some data into an array of character. I would like to be able to read the first 3bits of each characters and get fromthese bits a number between 0 and 7. Do you know how I can proceed?
Thank you!
You cannot read individual bits - you will have to use bitwise operators to infer their values.

The binary value 0111 is the decimal value 7.

Accordingly, if you bitwise AND a variable with the number 7, the number you are left with will be the bits you're interested in.

int output = input & 7;
you must write a function which will use Decimal To Binary math. read about it here
http://www.wikihow.com/Convert-from-Decimal-to-Binary

then send each element into this function, and this function will generate binary code for your data, then manipulate that data add substract do anything you wish.

or.... check this
http://www.daniweb.com/software-development/cpp/threads/5270 it says:

int main()
{
char x = 'a';
int y;

// the value of 'a' in hex is 0x61, or 97 decimal
// in binary, that would be: 0 1 1 0 0 0 0 1
// or 64 + 32 + 1, or 2^7 + 2^6 + 2^0
//
//loop for number of bits in a character
//this will print out bits in reverse order
//least significant bit first
for(y = 0; y < sizeof(char) * 8; y++)
printf("%c ", ( x & (1 << y) ) ? '1' : '0' );

puts("");
return 0;
}


kinda works for me... (code is not mine).
Topic archived. No new replies allowed.