Taking a single bit

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Taking a single bit

Post by mattallen37 »

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 ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Taking a single bit

Post by muntoo »

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
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
tcwan
Posts: 186
Joined: 30 Sep 2010, 07:39

Re: Taking a single bit

Post by tcwan »

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.
Is your intention to check/change a bitvalue in the original variable, or do you really want to extract the bitmap into separate variables?
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);
 
You can replace the bitmask with a variable value, but you have to make sure that it is in the correct bit position(s).
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Taking a single bit

Post by mattallen37 »

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 ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Taking a single bit

Post by mattallen37 »

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 ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Taking a single bit

Post by muntoo »

SaveBit():

Code: Select all

inline void SaveBit(byte &var, byte bit, bool value)
{
    var = (value ? var | (0x01 << bit) : var & ~(0x01 << bit));
}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests