简体   繁体   中英

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;

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();

Here's a more complete revision, note I commented out your saveBitMapFil and change the BYTE type because I don't know how to have them in my revision.

the key bit of code is this snippet, with a debug statement

N = 1;
for (int j = 7; j >=0; j--)  // I reversed you loop
{
     N = 1<<j;
     Index = ((Bitplane0_ROW[p] & N ? 1 :0)<<3)
           + ((Bitplane1_ROW[p] & N ? 1 :0)<<2)
           + ((Bitplane2_ROW[p] & N ? 1 :0)<<1)
           +  (Bitplane3_ROW[p] & N ? 1 :0);

     std::cout << " Index = " << std::bitset<4>(Index)  << std::endl;
#include <iostream>
#include <bitset>

using namespace std;

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;

    unsigned char* buf = new unsigned char[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 };

    int Index = 0;

    for (int p = 0; p < 8; p++)
    {
        N = 1;
        for (int j = 7; j >=0; j--)  // I reversed you loop
        {

            N = 1<<j;
            Index = ((Bitplane0_ROW[p] & N ? 1 :0)<<3)
                + ((Bitplane1_ROW[p] & N ? 1 :0)<<2)
                + ((Bitplane2_ROW[p] & N ? 1 :0)<<1)
                +  (Bitplane3_ROW[p] & N ? 1 :0);

            std::cout << " Index = " << std::bitset<4>(Index)  << std::endl;

            //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;
}

your code contained the first char

int Bitplane0_ROW[] = { 0b01100110
int Bitplane1_ROW[] = { 0b01111110
int Bitplane2_ROW[] = { 0b00000000
int Bitplane3_ROW[] = { 0b00000000

and the debug produces the same data pivoted

Index = 0000
Index = 1100
Index = 1100
Index = 0100
Index = 0100
Index = 1100
Index = 1100
Index = 0000

Just n = 8 b3 + 4 b2 + 2 b1 + b0 .

The retrieval of the bits can be done with

(n >> 3) & 1
(n >> 2) & 1
(n >> 1) & 1
n & 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM