Search code examples
c++binarybit

How Do I Combine 4 bit values to get a single integer


I spent hours trying to figure this out. I have four binary values that I want to combine into a single number. I got it working with two numbers but I need to get it working with four.

int Index = ((Bitplane0_ROW[p] & (1 << N)) >> N) | (((Bitplane1_ROW[p] & (1 << N)) >> N) << 1); // Works

I am stumped. Thanks in advance.

Edit.. Here is the complete program.

int main()
{
  
    int Bitplane0_ROW[] = { 0b01100110 , 0b11111111, 0b01011010, 0b01111110, 0b00000000, 0b10000001, 0b11111111, 0b01111110 }; // Array to to store numbers Last Row is first.
    int Bitplane1_ROW[] = { 0b01111110, 0b11111111, 0b11111111, 0b11011011, 0b11111111, 0b01111110, 0b00000000, 0b00000000 };
    int Bitplane2_ROW[] = { 0b00000000, 0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000};
    int Bitplane3_ROW[] = { 0b00000000, 0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000 };
    
    
    
    
    
  
    int N = 7; //to store bit
    int c = 0;

    BYTE* buf = new BYTE[8 * 5];

    unsigned char White[] = {255, 255, 255};
    unsigned char Green[] = {53, 189,104 };
    unsigned char Brown[] = {59,85,142 };
    unsigned char Tan[] = {154,194,237 };

    




    for (int p = 0; p < 8; p++)
    {
        for (int j = 0; j < 8; j++) // Row 6
        {
          
          
              
 
          
          int Index = ((Bitplane0_ROW[p] & (1 << N)) >> N) | (((Bitplane1_ROW[p] & (1 << N)) >> N) << 1); // Works
            if(Index == 0)
            {

                // Index 0 (White)
              //  Index = 0;
                buf[c + 0] = White[Index];
                buf[c + 1] = White[Index+1];
                buf[c + 2] = White[Index+2];

            }

            else if (Index == 1)
            {
                // Index 1 (Green)
                //Index = 0;
                buf[c + 0] = Green[Index];
                buf[c + 1] = Green[Index+1];
                buf[c + 2] = Green[Index+2];

            }
            else if (Index == 2)
            {

                // Index 2 (Brown)
                //Index = 0;
                buf[c + 0] = Brown[Index];
                buf[c + 1] = Brown[Index+1];
                buf[c + 2] = Brown[Index+2];
            }
            else if (Index == 3)
            {

                // Index 3 (Tan)
                Index = 0;
                buf[c + 0] = Tan[Index];
                buf[c + 1] = Tan[Index+1];
                buf[c + 2] = Tan[Index+2];
            }
            else if (Index == 15)
            {
                // Index 1 (Green)
                Index = 0;
                buf[c + 0] = Green[Index];
                buf[c + 1] = Green[Index+1];
                buf[c + 2] = Green[Index+2];

            }
            c += 3;
            N--;
        }
        N = 7;
    }

    SaveBitmapToFile((BYTE*)buf, 8, 8, 24, 0, "C:\\Users\\Chris\\Desktop\\Link_Sprite.bmp");

    delete[] buf;

return 0;


Solution

  • You can shift the bits manually or just use std::bitset:

    #include <bitset>
    
    // ...
    std::bitset<4> bs;
    bs.set(0, (Bitplane0_ROW[p] >> N) & 1);
    bs.set(1, (Bitplane1_ROW[p] >> N) & 1);
    bs.set(2, (Bitplane2_ROW[p] >> N) & 1);
    bs.set(3, (Bitplane3_ROW[p] >> N) & 1);
    unsigned long index = bs.to_ulong();