Home » Community » Newbie corner » Storing / Inserting Data per BIT
Re: Storing / Inserting Data per BIT [message #36060 is a reply to message #36059] |
Sun, 22 April 2012 17:02   |
Lance
Messages: 656 Registered: March 2007
|
Contributor |
|
|
check bit-wise operation
you will need
bit or |
bit and &
bit negation ~
and bit shift <<, >>
eg, if you want to set 2 most significant bits of a int16 without changing other bits, you can:
void test_bit()
{
uint16 target=0;
DUMP(target);
// set 2 most significant bits
target |= ((uint16)0x3 << 14);
// 0x3=0000 0000 0000 0011b right shift by 14 bits becomes 1100 0000 0000 0000b
// or 0xC000
DUMP(target);
// and set the 3 LSBs
target |= 0x7;
DUMP(target);
// if you instead want to unset the 2 least significant bits
// you can do something like:
target &= ~0x3;
// 0x3 is 0000 0000 0000 0011b
// ~0x3 becomes 1111 1111 1111 1100b
// bitwise-and this number with target result in the
// 2 LSB being unset.
DUMP(target)
}
[Updated on: Sun, 22 April 2012 17:06] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Mon Apr 28 11:15:31 CEST 2025
Total time taken to generate the page: 0.00883 seconds
|