I need to get the least significant bit of an unsigned char in C/C++.
can you give me an idea, how to solve this?
a C solution is what i prefer, but a C++ solution is also good.(:
Code: Select all
foo & 1Code: Select all
(i >> sizeof(i)) & 1
Not exactly as sizeof of an [unsigned] char is always 1 so you are shifting only one bit.Salmon wrote:There is probably a better way to do it.Code: Select all
(i >> sizeof(i)) & 1
Code: Select all
foo >> sizeof(foo) * CHAR_BIT - 1 & 1;
Code: Select all
msb = (foo >> 7) & 1;
In C99 this is guaranteed.KAZZQE wrote:but..
..sizeof(an unsigned char) is 1.
Nope, you can find DSP where CHAR_BIT is 16 or even 32.KAZZQE wrote:..CHAR_BIT is 8.