I am looking to learn how to get two nibbles (high and low) from a byte using C# and how to assembly two nibbles back to a byte.
I am using C# and .Net 4.0 if that helps with what methods can be done and what libraries may be available.
I am looking to learn how to get two nibbles (high and low) from a byte using C# and how to assembly two nibbles back to a byte.
I am using C# and .Net 4.0 if that helps with what methods can be done and what libraries may be available.
You can 'mask off' 4 bits of a byte to have a nibble, then shift those bits to the rightmost position in the byte:
byte x = 0xA7; // For example...
byte nibble1 = (byte) (x & 0x0F);
byte nibble2 = (byte)((x & 0xF0) >> 4);
// Or alternatively...
nibble2 = (byte)((x >> 4) & 0x0F);
byte original = (byte)((nibble2 << 4) | nibble1);
I would assume you could do some bitwise operations
byte nib = 163; //the byte to split
byte niblow = nib&15; //bitwise AND of nib and 0000 1111
byte nibhigh = nib&240; //bitwise AND of nib and 1111 0000
Assert.IsTrue(nib == nibhigh|niblow); //bitwise OR of nibhigh and niblow equals the original nib.