in c++ how do you set the first 4 bits in a byte

Discussion about everything. New games, 3d math, development tips...
Post Reply
Adversus
Posts: 128
Joined: Sun Oct 05, 2008 10:58 pm
Contact:

in c++ how do you set the first 4 bits in a byte

Post by Adversus »

I have two integer alpha values and I want to set the first four bits of a byte to one and the second 4 bits of the byte to the second alpha.

I think I need to use bitwise operators but can't figure it out. Does anyone else know how?
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Re: in c++ how do you set the first 4 bits in a byte

Post by greenya »

Code: Select all

            int varResult = 0;
            int varWith4bitsHI = 0xa;
            int varWith4bitsLO = 0xb;
 
            varResult = varWith4bitsHI << 4 | varWith4bitsLO;
            // now varResult == 0xab
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: in c++ how do you set the first 4 bits in a byte

Post by hendu »

value = secondalpha;
value |= 15;

@greenya
0xb would be 11 in decimal or 1011 in binary, not all four 1111 = 15 = 0xf
Adversus
Posts: 128
Joined: Sun Oct 05, 2008 10:58 pm
Contact:

Re: in c++ how do you set the first 4 bits in a byte

Post by Adversus »

thanks, what would be the functions for adding and getting them individually?
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Re: in c++ how do you set the first 4 bits in a byte

Post by greenya »

hendu wrote:@greenya
0xb would be 11 in decimal or 1011 in binary, not all four 1111 = 15 = 0xf
Didn't understand you.
4 bits is 1111 and value range is 0-15 inclusive.
8 bits makes byte.
In my post i showed how individually set first 4 bits and last 4 bits.
Adversus wrote:thanks, what would be the functions for adding and getting them individually?
Refering to my code above, you can do that next way:

Code: Select all

            int varResult11110000 = (varResult & 0xf0) >> 4; // now its 0xa
            int varResult00001111 = varResult & 0xf; // now its 0xb
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: in c++ how do you set the first 4 bits in a byte

Post by hendu »

Yep, but he asked for all 4 low bits set ;)
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: in c++ how do you set the first 4 bits in a byte

Post by Mel »

the bitwise operations that may help are the shift left and shift right ops. But i never remember how they work exactly, and leads me to errors sometimes, the good thing is that both have an equivalent in multiplying or dividing a number by the power of 2 you want to shift the bits, if you want to store your alpha values, this would work pretty well:

combinedAlpha = alpha1*16 + alpha2;

if you want to recover your original alphas:

alpha1 = combinedAlpha/16;
alpha2 = combinedAlpha%16;

and you are done. With integer values they are as fast as the bitwise operations. Just keep in mind that your combined alpha should be treated as an unsigned char.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Re: in c++ how do you set the first 4 bits in a byte

Post by zerochen »

or u use something like this

Code: Select all

 
    void setBit(int& val, int bitToSet)
    {
        val |=  1<<bitToSet;
    }
 
    bool isBitSet(int& val, int bitToTest)
    {
        return (1<<bitToTest) == (val &&  1<<bitToTest);
    }
 
Adversus
Posts: 128
Joined: Sun Oct 05, 2008 10:58 pm
Contact:

Re: in c++ how do you set the first 4 bits in a byte

Post by Adversus »

Thanks, bit help. I think I even understand what that does :-)
Post Reply