Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine 2 numbers in a byte

I have two numbers (going from 0-9) and I want to combine them into 1 byte. Number 1 would take bit 0-3 and Number 2 has bit 4-7.

Example : I have number 3 and 4.
3 = 0011 and 4 is 0100.
Result should be 0011 0100.

How can I make a byte with these binary values?

This is what I currently have :

    public Byte CombinePinDigit(int DigitA, int DigitB)
    {
        BitArray Digit1 = new BitArray(Convert.ToByte(DigitA));
        BitArray Digit2 = new BitArray(Convert.ToByte(DigitB));

        BitArray Combined = new BitArray(8);
        Combined[0] = Digit1[0];
        Combined[1] = Digit1[1];
        Combined[2] = Digit1[2];
        Combined[3] = Digit1[3];  

        Combined[4] = Digit2[0];
        Combined[5] = Digit2[1];
        Combined[6] = Digit2[2];
        Combined[7] = Digit2[3];
    }

With this code I have ArgumentOutOfBoundsExceptions

like image 724
Robby Smet Avatar asked Apr 25 '13 12:04

Robby Smet


People also ask

How many combinations in a byte?

A byte is a group of 8 bits. A bit is the most basic unit and can be either 1 or 0. A byte is not just 8 values between 0 and 1, but 256 (28) different combinations (rather permutations) ranging from 00000000 via e.g. 01010101 to 11111111 . Thus, one byte can represent a decimal number between 0(00) and 255.

What is 2 byte binary representation?

Two bytes means 16 bits (binary digits), which means that you can represent 65,536 different combinations (2 to the power of 16)! That's a whole lot of different permutations that you can represent with just 2 bytes!

How many numbers can 1bit hold?

A bit is a binary digit, the smallest increment of data on a computer. A bit can hold only one of two values: 0 or 1, corresponding to the electrical values of off or on, respectively. Because bits are so small, you rarely work with information one bit at a time.

Is an element of a byte that can represent one of two values?

The bit is the most basic unit of information in computing and digital communications. The name is a portmanteau of binary digit. The bit represents a logical state with one of two possible values.


2 Answers

Forget all that bitarray stuff.

Just do this:

byte result = (byte)(number1 | (number2 << 4));

And to get them back:

int number1 = result & 0xF;
int number2 = (result >> 4) & 0xF;

This works by using the << and >> bit-shift operators.

When creating the byte, we are shifting number2 left by 4 bits (which fills the lowest 4 bits of the results with 0) and then we use | to or those bits with the unshifted bits of number1.

When restoring the original numbers, we reverse the process. We shift the byte right by 4 bits which puts the original number2 back into its original position and then we use & 0xF to mask off any other bits.

This bits for number1 will already be in the right position (since we never shifted them) so we just need to mask off the other bits, again with & 0xF.

You should verify that the numbers are in the range 0..9 before doing that, or (if you don't care if they're out of range) you can constrain them to 0..15 by anding with 0xF:

byte result = (byte)((number1 & 0xF) | ((number2 & 0xF) << 4));
like image 100
Matthew Watson Avatar answered Oct 02 '22 11:10

Matthew Watson


this should basically work:

byte Pack(int a, int b)
{
    return (byte)(a << 4 | b & 0xF);
}

void Unpack(byte val, out int a, out int b)
{
    a = val >> 4;
    b = val & 0xF;
}
like image 41
Robert J. Avatar answered Oct 02 '22 12:10

Robert J.