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
Best way to access nibbles
Moderator: Stef
-
- Very interested
- Posts: 118
- Joined: Mon Feb 19, 2018 7:31 pm
Re: Best way to access nibbles
Union-struct is the nicest way, but nibble access won't be fast no matter what.
-
- Very interested
- Posts: 3131
- Joined: Thu Nov 30, 2006 9:46 pm
- Location: France - Sevres
- Contact:
Re: Best way to access nibbles
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:
To change nibble 2 for instance
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;