Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you nibble (nybble) bytes in C#?

Tags:

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.

like image 707
Rodney S. Foley Avatar asked Jun 26 '10 18:06

Rodney S. Foley


Video Answer


1 Answers

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); 
like image 148
Mau Avatar answered Sep 28 '22 00:09

Mau