Hi, my problem is I know little about bits bytes etc and how to manipulate them in c/c++ - please could someone give me some info or links, thanks!
Printable View
Hi, my problem is I know little about bits bytes etc and how to manipulate them in c/c++ - please could someone give me some info or links, thanks!
As you probably know, one 'bit' is the smallest-definable amount of computer memory, and consists of two possible states: '1' (ON) and '0' (OFF).
Bits are further arranged into higher collections:
4 BITS: one NYBBLE
8 BITS: one BYTE
16 BITS: one WORD
32 BITS: one DWORD (double-word)
64 BITS: one QWORD (quad-word)
these are all you need to know at this time, since 32-bit computing is all most of us know so far...
as for manipulation in C++, this is very easy, so long as you get around the terminology. basically, just think of a BYTE as a series of BITS, clumped together in sets of 4 (nybbles):
H<- [ 0101 0100 ] ->L
Furthermore, the right-most bit is called the 'low-order' bit, with the bit to the far-left called the 'high-order' bit.
Manipulation in C++ is done through SHIFTING (using the << and >> operators). ** just don't confuse these with the IOSTREAM insertion and extraction operators!! **
shifting left shifts ALL bits in the series left by x amount, specified on the right-hand-side of the operator (the operand). Becuase we are working with binary (base 2), instead of base 10, we will find that this MULTIPLIES the number by 2 (123 * 10 == 1230); (123 << 1 == 246). for example:
//---------------------------------------------------------------------------------
int nNumber = 123;
cout << nNumber << endl;
nNumber << 1; // Multiply nNumber by 2
cout << nNumber << endl;
//---------------------------------------------------------------------------------
Thus, diagramatically, this is represented:
int nNumber = 123;
[ 0111 1011 ] : 123
nNumber << 1;
[ 1111 0110 ] : 246
i saw this in a post earlier: what happens to the 'lost' bit? it gets shifted into the Carry Flag (but this is irrelevant, unless you use inline ASM)
likewise, the same happens for shifting RIGHT (>>), except that numbers are DIVIDED by 2, and the remainder discarded. well, this is basic bit manipulation, but just remember, that when shifting, if you want to multiply by a number, that number will be 2^(n-1):
left shift of 0 == MUL by 1
left shift of 1 == MUL by 2
left shift of 3 == MUL by 4
left shift of 4 == MUL by 8
<...>
secondly, you need to know how to use AND, OR, NOT and XOR logic. you should know the basics already, as it expands just a little on what i showed you above. you will find that you won't use it much in C++, as all this sort of work has been covered for you by libraries. but you will still need it sometimes:
&: 0010 1101 , 1010 0010 -> 0010 0000 AND
| : 0010 1101 , 1010 0010 -> 1010 1110 OR
^: 0010 1101 , 1010 0010 -> 0010 0000 XOR (please verify)
! : 0010 1101 -> 1101 0010 NOT (one operand!)
AND is useful for testing bits. again, you will NOT need to do this, as arrays are easier to implement than bitfields in C++, and with FLAGS, MFC provides some 'nice' functions to take care of all this for you.
OR combines all '1' bits, regardless of their state.
NOR combines '1's only if both states are NOT '1'. if so, '0' results.
NOT inverts all bits in the field.
a common pitfall is to mistake the above symbols for their logical testing counterparts (i.e. ! versus !!). make sure this is not you, as total chaos WOULD ensue :)
that's all the bit manip. you'll need in your fledgling days, until you try something great like Assembly coding (a recommended MUST DO for any *serious* coder). THAT's when you learn to do bit-state comparisons, masking and so on...if you want to know how, just post it, and i will reply. happy coding.........
Regards,
Peter Kimberley