Page 1 of 1

Best way to access nibbles

Posted: Thu Feb 03, 2022 2:44 pm
by cloudstrifer
Hi!

I want to read or write a nibble in U16 variable, for example, nible 0 = 12 or nible 3 = 1.
Can I access 4 bits individualy with good a performance?

__3___2____1___0
0000-1111-0000-1111

Re: Best way to access nibbles

Posted: Thu Feb 03, 2022 4:08 pm
by cero
Union-struct is the nicest way, but nibble access won't be fast no matter what.

Re: Best way to access nibbles

Posted: Fri Feb 04, 2022 8:44 am
by Stef
You can also use bit fields in a structure, but as Cero said, nibble manipulation aren't really fast.
I think that using boolean logic operation directly on your u16 variable is the fastest way to go:

Code: Select all

var = (var & 0xF0FF) | 0x0300;
To change nibble 2 for instance