How do I manipulate nibbles?

I want to be able to read and write nibbles. Also how could I make sure that all my nibbles go into a file an even number of times so that there is no crash(or anything like that)?
How would I go about this?
Thanks
Last edited on
I've often wondered this my self, it think it would require some serious low level control of the bit. if it's even possible
You can simply use bit shifting to set whichever bits you like, e.g. with the following functions:

1
2
3
4
5
6
7
8
9
10
template <class I> void setNibble(I& o,int index,I nibbleVal)
{
  o&=~(15<<(index*4));     //blank nibble
  o|=nibbleVal<<(index*4); //set new value
}

template <class I> I getNibble(const I& o,int index)
{
  return (o&(15<<(index*4)))>>(index*4);
}


Integrate them into a class and you're done.
Last edited on
Er, your file must always be a multiple of two nibbles long... so you will have to consider that when you access the file.
Topic archived. No new replies allowed.