Pages: [1]   Go Down
Author Topic: No such thing as a 'Nibble' in C++?  (Read 1224 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm fairly new to Arduino and I am currently working with a Serial GPS receiver.

I receive some returned bytes such as firmware versions as a byte, however the higher 2 bits form the major revision, then there should be a decimal place, then the lower 2 bits form the minor revision number.

Please can anyone help me or point me in the right direction to split bytes in 2?  The bit() command only seems to allow you to look at individal bits, when I need a 'nibble', 2 bit size.

Many Thanks,
Morrolan
Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I always thought of nibbles (or nybbles) as being half a byte, or four bits.

hint: if you can read a single bit, you're halfway there.
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Groove,

I'm sorry you are correct, a nibble is 4 bits, allowing values between 0 - 15.

Maybe it is my lack of understanding of C++, but how would one add the four individual bits together form 1 single nibble-sized number?

Many Thanks,
Morrolan  
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
typedef struct {
  unsigned low:4;
  unsigned high:4;
} nibble;

nibble test = { 3, 6 };

void setup() {
  Serial.begin(9600);
  Serial.println(test.low);
  Serial.println(test.high);
}
void loop(){}
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Assuming you are trying to decompose a received byte into two nibbles:

Code:
byte data;

byte lownibble = data & B00001111;

byte highnibble = data >> 4;
« Last Edit: October 08, 2010, 04:50:51 pm by lefstin » Logged

0
Offline Offline
Sr. Member
****
Karma: 5
Posts: 476
what?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im with Professor Chaos on this one, you could use some inline asm also such as
swapf, data
which swaps the high nib and low nib in a byte, it is useful when talking to 4bit lcd's
Logged

Van Alstyne, TX
Offline Offline
Full Member
***
Karma: 1
Posts: 154
Haven't smoked an Arduino... yet.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And I don't believe that there's anything in the C or C++ language
references that would guarantee that the 'high' bitfield would be aligned
with the four most significant bits nor the 'low' bitfield being aligned with
the four least significant bits.  Maybe on the Arduino it might work....
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Maybe on the Arduino it might work....
It does.  It works well.

For those concerned with performance... certain combinations of bit fields will generate several machine instructions.  AlphaBeta's example is fairly lean.  I think it's the same as using shift-and.
« Last Edit: October 09, 2010, 12:14:01 am by bcook » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6379
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The compiler is actually smart enough to use swapf when doing bit pieces of 4 bits.  Sometimes.  C does better than C++...

(http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=752634#752634 )
Logged

England
Offline Offline
Full Member
***
Karma: 0
Posts: 211
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://softsolder.com/2009/11/23/arduino-bit-fields-start-low/

bitfields and unions are incredibly useful in uC programming.
« Last Edit: October 09, 2010, 06:29:09 am by UltraMagnus » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24454
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
bitfields and unions are incredibly useful in uC programming
And also incredibly irritating if you inadvertently use the same union at opposite ends of a link with different endianness.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

England
Offline Offline
Full Member
***
Karma: 0
Posts: 211
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And also incredibly irritating if you inadvertently use the same union at opposite ends of a link with different endianness.

perhaps, but that is nothing that cannot be solved by a quick lookup table.
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6379
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
nothing that cannot be solved by a quick lookup table.
IIRC, the IP "Fragment offset" field can not be represented as a C bitfield on a little-endian CPU/compiler.  It spans the byte boundry on a big-endian system, but ends up discontiguous on little-endian systems.  So there are things that can't be solved with a lookup table...

:-)
Logged

Pages: [1]   Go Up
Jump to: