I said logical not bitwise, I do things like that sometimes, sorry.
Also, a warning... don't C++ify the structs with methods. I think that will break things. Use the struct as a POD/Base type in a class, that is fine, but don't make IT the object.
I was thinking that if you had to ask, you might not know how to use what I said.
or, in english, CP is a pointer to a byte that has 2 nibbles :)
Note that 'a' is the low order bits, and 'b' is the high order bits. But it will print backwards in hex, because of this, though logically a is first. Right?
You can also avoid the pointer mess with a union...
union u
{
nibs n;
unsigned char c;
};
u x;
x.n.a = 10;
x.n.b = 11;
/// x.c is your byte, without the pointer magic. it is "cp[0]" above, conceptually.
I *think* (wow, this has been a long while) that intel assembler can directly access nibbles via al and ah half registers? Not 100% sure, but if I recall it right you have eax (32?), ax (16), a (8), and al and ah (4 bits each). A couple of lines of assembler could do this, if you wanted to really go off on a tangent!
Bitfields are useless (at least for my purposes) thanks to that passage. It's either bitfields or unions (since reading the non-active variant member causes undefined behavior) which have my vote for the least-useful core language feature. I digress.
It's fine to make class types with bit-field members non-POD as long as you don't later assume they're not (although that assumption is quite usual for such a class).
Right. I agree, but in practice, I have yet to find this theoretical compiler that does not work on the above code as I laid it out. Someone here probably knows of one, I don't use that many different compilers.
Honestly, the idea of a nibble is useless to me unless packing something up for an embedded system that wants 2 per byte in its communications protocol. There are not a whole lot of those left in the world either.
If your compiler works as above, its probably the cleanest way to do the nibble thing. If not, you can write a bunch of shifting and /or code or as I said, a handful of assembly lines gets you there non-portable style. Its easy to test if it works. Run it, and see if your struct is size 1 :) If not, you need to do something else.
if I recall it right you have eax (32?), ax (16), a (8), and al and ah (4 bits each).
Aren't al and ah the low byte and high byte of ax? I don't recall 4-bit direct access on x86, but like you, it's been a looooonnng time.
Trivia: HP used a 4-bit processor in it's calculators for a long time. Each address referred to a 4-bit nibble. They used this type of processor (or an emulator of it) right up through the HP-50g which was recently discontinued. The HP-12C, which has been in continuous production for something like 30 years, still uses it, although via an emulator. https://en.wikipedia.org/wiki/HP_Saturn
Damn! A new-in-box 11C might be more valuable than you think. If you ever want to sell it, pop over to hpmuseum.org and post it in the classified section. That's where all the HP calculator nerds hang out :)
I don't think any amount would part me with that thing. You can't get a decent RPN calculator anymore. I could let the 15 go, but never an 11. They used to buy my dad a new one ever year or and he could keep the old one as devalued/destroyed equipment. So he brought them home. So I now have 2 11s and a 15, + my own GX or whatever it was with the 3-d plotting from the 90s. All but the one 11 are effectively new. Maybe I can retire now lol, sitting on a gold mine..