Q: Best way to do this?

S

scooby.doo

Hi,

I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on. Now I don't know the
correct terminology for what I'm trying to do, so that hasn't helped
me looking in reference manuals or the Internet. The only way I can
see of doing it is:-


unsigned char values[7] = {1, 3, 7, 15, 31, 63, 127};

main ()
{
unsigned char i;

for (i = 0; i < 8; i++) use_byte(values);


}

I do not need to worry about the last bit as if the byte is full (i.e.
decimal 8) that is handled differently.


Is this a sensible solution?

Thank-you.
 
M

Malcolm McLean

Hi,

I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on. Now I don't know the
correct terminology for what I'm trying to do, so that hasn't helped
me looking in reference manuals or the Internet. The only way I can
see of doing it is:-


unsigned char values[7] = {1, 3, 7, 15, 31, 63, 127};

main ()
{
unsigned char i;

for (i = 0; i < 8; i++) use_byte(values);


}

I do not need to worry about the last bit as if the byte is full (i.e.
decimal 8) that is handled differently.


Is this a sensible solution?

It is OK, but not terribly efficient.
You need to construct a variable like 00000111, and then OR, to set the last
three bits.
So
unsigned char mask = 0xFF << decimal;
mask = ~mask;
x |= mask;

will do it quite nicely. On a really simple processor the << decimal will be
a hidden loop, but it should be just one or two instructions on anything
that does any serious processing.
 
M

Martin Wells

scooby:
I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on.


First attempt would be something like:


typedef unsigned SomeIntType;

SomeIntType TurnOnBits(unsigned quantity)
{
SomeIntType result = 0;

while(quantity--)
{
result <<= 1;
result += 1;
}

return result;
}

Martin
 
P

pete

Hi,

I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on. Now I don't know the
correct terminology for what I'm trying to do, so that hasn't helped
me looking in reference manuals or the Internet. The only way I can
see of doing it is:-

unsigned char values[7] = {1, 3, 7, 15, 31, 63, 127};

main ()
{
unsigned char i;

for (i = 0; i < 8; i++) use_byte(values);


}


You can replace (values), with (1U << i) - 1).
I do not need to worry about the last bit as if the byte is full (i.e.
decimal 8) that is handled differently.


Is this a sensible solution?

Yes.
 
C

Charlie Gordon

Hi,

I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on. Now I don't know the
correct terminology for what I'm trying to do, so that hasn't helped
me looking in reference manuals or the Internet. The only way I can
see of doing it is:-


unsigned char values[7] = {1, 3, 7, 15, 31, 63, 127};

if the index into this array is the decimal value mentioned above, you got
it wrong: C arrays are 0 based, so you should make the array at least 8
elements long (0 thru 7) or preferably 9 (0 thru 8 inclusive).

should be: int main (void)
{
unsigned char i;

better make the loop index an int.
for (i = 0; i < 8; i++) use_byte(values);


OOPS: you access 8 elements from an array of 7 ;-) see above

should return 0.
I do not need to worry about the last bit as if the byte is full (i.e.
decimal 8) that is handled differently.

but it cannot hurt to have a generic solution that handles all cases.
Is this a sensible solution?

providedyou fix the array size and contents, it works, but it is neither
efficient nor safe:
- initializing the array with the values of 2 to the n - 1 by hand is error
prone, just imagine if you needed 32 values.
- there is a much simpler solution with the bit shift operator:

the bits you want to set can be computed very efficiently as ((1 << n) - 1)
for values of n between 0 and 2 less than the size of an int, amply
sufficient for your needs.

you then use the bitwise or to set them into the word:

value |= ((1 << n) - 1);
 
P

Phoenix

Hi,

I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on. Now I don't know the
correct terminology for what I'm trying to do, so that hasn't helped
me looking in reference manuals or the Internet. The only way I can
see of doing it is:-

unsigned char values[7] = {1, 3, 7, 15, 31, 63, 127};

main ()
{
unsigned char i;

for (i = 0; i < 8; i++) use_byte(values);

}

I do not need to worry about the last bit as if the byte is full (i.e.
decimal 8) that is handled differently.

Is this a sensible solution?

Thank-you.




yes, it is a good idea to use '|' to make this.
further more, if you want to change a certain bit to opposite, using
xor, '^'.
I hope there's something useful for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top