user3475003
user3475003

Reputation: 155

Combining three bits to get a single integer value

So I have three variables each hold a 1 or a 0. These three form an address 0 - 7 when combined. for example var1 = 1; var2 = 0; var3 = 1;

would be 5.

How would I go about combining these three variable to get an integer value? I have heard bit shifting operations would be the best way but I'm not sure how to do it. thanks.

Upvotes: 0

Views: 1181

Answers (6)

Clifford
Clifford

Reputation: 93534

If the input variables are guaranteed to have value 1 or 0 then:

address = (var1 << 2) | (var2 << 1) | var3 ;

will do exactly what you want; however given that var1 etc. are integers an invalid and likely out-of-range address will be generated if the preconditions are not checked or limited.

A more flexible and maintainable implementation that allows simple address range extension would be to use an array rather than three separate variables. The fact that you numbered then in the first place rather suggests an array might be appropriate in any case):

#define ADDR_BITLEN 3
unsigned var[ADDR_BITLEN] ;

...

unsigned address = 0 ;
for( int bit = ADDR_BITLEN - 1; bit >= 0; bit++ )
{
    address |= ((var[bit] == 0) ? 0 : 1) << bit ;
}

Now any number of bits up-to the bit length of an unsigned may be used for address simply by changing ADDR_BITLEN, further more the loop body ensures that an invalid address is not generated if address_bits[bit] is > 1 by selecting 0 or 1 explicitly, which would be somewhat cumbersome and hard to read and maintain if the array and loop were not used.

Upvotes: 0

When you bit shift some number you will just go from right to left on the memory. So, as we know, if we shift the number to the left it'll be multiplied by 2, since the base of the computer is binary. If we shift to the right, it'll be divided by 2.

like this:

decimal 5 --> binary 00101 decimal 10 --> binary 01010

Note that we've just shifted once to the left and we have the result of 5 multiplied by 2, therefore 10.

In your code I suggest you receive the variables by an scanf, the proceed to shift those and then sum them, like so:

#include <stdio.h>

int main(){
    int var1, var2, var3;
    scanf("%d %d %d", &var1, &var2, &var3);
    printf("%d\n", (var1 << 2) + (var2 << 1) + var3);
    return 0;
}

The code above we put the variable__<<__number_of_shifts. So in the first one we shift the var1 twice, the second one we shift once and at the last one we shift none. Then we sum all together and have your answer!

Upvotes: 0

Mike -- No longer here
Mike -- No longer here

Reputation: 2092

In order to get a true answer you need to define what variable belongs to what bit position in the binary number you're trying to calculate and make sure you set the value of each variable to either zero or one or you'll be shifting the wrong bits around in the result.

In the code example below, the order of bits read from left to right is var3, var2, var1. var3 is known as the most significant bit, and var1 is known as the least significant bit.

If you want the value of 1, you set var1 to 1, then var2 and var3 to 0. If you want the value of 4, you set var1 and var2 to 0 then set var3 to 1. If you want the value of 5, you set var1 and var3 to 1 then set var2 to 0.

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int var1=1;
    int var2=0;
    int var3=1;
    int res=(var3 << 2) + (var2 << 1) + var1;
    printf("%d\n",res);
    return 0;
}

If you want a somewhat easier-to-understand version, you can use this code:

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int LeastSignificantBit=1;
    int MiddleBit=0;
    int MostSignificantBit=1;
    int Number=0;
    Number=(MostSignificantBit << 2) + (MiddleBit << 1) + (LeastSignificantBit << 0);
    printf("Number is %d\n",Number);
    return 0;
}

I understand using << 0 is overkill, but I'm illustrating the fact that the least significant bit doesn't need to be shifted over.

Upvotes: 1

user3376587
user3376587

Reputation: 134

var1 = 1, var2 = 0, var3 = 1, var4 = 1, var5 = 0, var6 = 0, var7 = 1, var8 = 0

byte = (var1<<7)+(var2<<6)+(var3<<5)+(var4<<4)+(var5<<3)+(var6<<2)+(var7<<1)+var8;

value of byte is 10110010

Upvotes: 3

Samidamaru
Samidamaru

Reputation: 1081

This works:

You shift the first bit 2 bits to the left with << 2, the second bit 1 bit to the left with << 1 and leave the last bit as it's in the last place. That gives you 3 variables which are now 100, 000 and 001, You then or them together with | to get 101 i.e. 5.

I.e.

v1 is currently 001 in binary and you want it to be the 3rd bit, I.e. 100, so you need to shift it 2 to the left (v1 << 2)

Likewise, v2 is currently 000, and you want the 0 to be the 2nd bit, so you shift it 1 to the left (v2 << 1) to give 000 (trivial in this case but obviously different with a 1 here).

Finally, v3 is 001 and you want it to be the 3rd bit... It already is so we can leave it where it is.

Finally to combine them, we or the values together with |

100 |

000 |

001 =

101 = 5 in binary

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int v1=1, v2=0, v3=1;

    int result;

    result = (v1 << 2) | (v2 << 1) | v3;

    printf("%d\n", result);

    return 0;
}

Upvotes: 1

MikeCAT
MikeCAT

Reputation: 75062

  1. Make sure the values are 0 or 1 for safety: AND with 1 do this
  2. move these 0/1 values to proper positions: done with << operator
  3. combine them: OR these values

if var1 = 1; var2 = 0; var3 = 0 should be 4, use this:
((var1 & 1) << 2) | ((var2 & 1) << 1) | (var3 & 1)

if var1 = 1; var2 = 0; var3 = 0 should be 1, use this:
((var3 & 1) << 2) | ((var2 & 1) << 1) | (var1 & 1)

Upvotes: 4

Related Questions