Taking a single bit
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Taking a single bit
In NXC, is there a "good" way to assign a variable value to the value of a specific bit in another variable? For example, say there is a byte with the value of 1001 0100, and I want to have it broken up into 8 separate variables, each one representing a single bit. Another option is to but it into an array format, either way is fine.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: Taking a single bit
Is this what you want?
Code: Select all
bool GetBit(byte var, byte bit)
{
return((var >> bit) & 0x01);
}
// 0x42 == 00101010
// GetBit(0x42, 7) == 0
// GetBit(0x42, 6) == 0
// GetBit(0x42, 5) == 1
// GetBit(0x42, 4) == 0
// GetBit(0x42, 3) == 1
// GetBit(0x42, 2) == 0
// GetBit(0x42, 1) == 1
// GetBit(0x42, 0) == 0
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: Taking a single bit
Is your intention to check/change a bitvalue in the original variable, or do you really want to extract the bitmap into separate variables?mattallen37 wrote:In NXC, is there a "good" way to assign a variable value to the value of a specific bit in another variable? For example, say there is a byte with the value of 1001 0100, and I want to have it broken up into 8 separate variables, each one representing a single bit. Another option is to but it into an array format, either way is fine.
It is rather expensive to store a bit value in a single variable. If you just want to check/change the value of a bit, define a bitmask, then use AND, OR and XOR logic functions to access the variable.
Code: Select all
/* Assume 8 bit variables for convenience, extend the bitmask definitions as necessary */
#define BIT0 0x0001
#define BIT1 0x0002
#define BIT2 0x0004
#define BIT3 0x0008
#define BIT4 0x0010
#define BIT5 0x0020
#define BIT6 0x0040
#define BIT7 0x0080
int aVar, int bitValue;
Checking a bit value @ BIT7:
bitValue = (aVar | BIT7); /* Then check if bitValue is zero or non-zero */
Clearing a bit value @ BIT5:
aVar = (aVar & ~BIT5); /* Note, to clear, you AND the bit with the inverse BITMASK */
Setting a bit value @ BIT3:
aVar = (aVar | BIT3);
Toggling a bit value @ BIT0:
aVar = (aVar ^ BIT0);
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: Taking a single bit
Ah, thank you both. It looks like either suggestion should work. Now I will try them, and confirm.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: Taking a single bit
Thank you, it does work.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: Taking a single bit
SaveBit():
Code: Select all
inline void SaveBit(byte &var, byte bit, bool value)
{
var = (value ? var | (0x01 << bit) : var & ~(0x01 << bit));
}
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Who is online
Users browsing this forum: No registered users and 2 guests