Best way to access nibbles

SGDK only sub forum

Moderator: Stef

Post Reply
cloudstrifer
Very interested
Posts: 118
Joined: Mon Feb 19, 2018 7:31 pm

Best way to access nibbles

Post by cloudstrifer » Thu Feb 03, 2022 2:44 pm

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

cero
Very interested
Posts: 338
Joined: Mon Nov 30, 2015 1:55 pm

Re: Best way to access nibbles

Post by cero » Thu Feb 03, 2022 4:08 pm

Union-struct is the nicest way, but nibble access won't be fast no matter what.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Best way to access nibbles

Post by Stef » Fri Feb 04, 2022 8:44 am

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

Post Reply