Velocity Reviews > Q: Best way to do this?

# Q: Best way to do this?

scooby.doo@shaggy.to
Guest
Posts: n/a

 09-10-2007
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]);

}

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

Is this a sensible solution?

Thank-you.

Malcolm McLean
Guest
Posts: n/a

 09-10-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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]);
>
>
> }
>
> I do not need to worry about the last bit as if the byte is full (i.e.
> decimal 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;

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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Martin Wells
Guest
Posts: n/a

 09-10-2007
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

pete
Guest
Posts: n/a

 09-10-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> 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]);
>
>
> }

You can replace (values[i]), with (1U << i) - 1).

> I do not need to worry about the last bit as if the byte is full (i.e.
> decimal that is handled differently.
>
>
> Is this a sensible solution?

Yes.

--
pete

Charlie Gordon
Guest
Posts: n/a

 09-11-2007
<(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> 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).

> main ()

should be: int main (void)

> {
> unsigned char i;

better make the loop index an int.

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

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 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

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

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

--
Chqrlie.

PS: you can use ((1U << n) - 1) for an unsigned result upto 1 less than the
size of an unsigned int.

Phoenix
Guest
Posts: n/a

 09-11-2007
On Sep 11, 5:39 am, (E-Mail Removed) wrote:
> 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]);
>
> }
>
> I do not need to worry about the last bit as if the byte is full (i.e.
> decimal 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.