Velocity Reviews > Set bits

# Set bits

s
Guest
Posts: n/a

 10-09-2003
If I have:

unsigned char value = 0xBD;
unsigned char 2bits = 0x02;

How do I set the two MSB in value to the two LSB in 2bits without
changing any other bits in value?

<mytry>
value &= 0x3F; //set those two bits to zero
value |= (2bits<<6);
</mytry>

Do I have to clear(or set) those two bits first?

Frank Roland
Guest
Posts: n/a

 10-09-2003
"s" <youshouldbe@home> schrieb im Newsbeitrag
news:<3F85BE28.1020901@home>...

> If I have:

>

> unsigned char value = 0xBD;

> unsigned char 2bits = 0x02;

>

> How do I set the two MSB in value to the two LSB in 2bits without

> changing any other bits in value?

>

> <mytry>

> value &= 0x3F; //set those two bits to zero

> value |= (2bits<<6);

> </mytry>

Use the now identifier in your try and it should work.

Kind regards,

s
Guest
Posts: n/a

 10-09-2003

Here's a better explanation of what I'm really trying to do:

unsigned char target = 0xBD;
unsigned char small_value = 2;
unsigned position = 0xC0;

I need to put the two bits in small_value into target at the place
indicated by the set bits in position.

thanks

Frank Roland wrote:
>>value |= (2bits<<6);

> Use the now identifier in your try and it should work.
>
> Kind regards,
>
>

Tim Hagan
Guest
Posts: n/a

 10-09-2003
s wrote:
>
> If I have:
>
> unsigned char value = 0xBD;
> unsigned char 2bits = 0x02;

2bits is an invalid identifier. The first character must be a letter or
an underscore, followed by any sequence of digits and/or upper or lower
case letters.

> How do I set the two MSB in value to the two LSB in 2bits without
> changing any other bits in value?
>
> <mytry>
> value &= 0x3F; //set those two bits to zero
> value |= (2bits<<6);
> </mytry>

Looks good to me. However, it isn't very interesting, since the two
bits that you are trying to set in 'value' are already set you way
want them. Observe:

0xBD = 10111101
^^
0x02 = 00000010

> Do I have to clear(or set) those two bits first?

You must clear them first, as you did in your example.

--
Tim Hagan

Tim Hagan
Guest
Posts: n/a

 10-10-2003
s wrote:
>
> Here's a better explanation of what I'm really trying to do:
>
> unsigned char target = 0xBD;
> unsigned char small_value = 2;
> unsigned position = 0xC0;
>
> I need to put the two bits in small_value into target at the place
> indicated by the set bits in position.

Here's one way to do it. It is not a very elegant solution, nor is the
output particularly interesting.

#include <stdio.h>

int main(void)
{
unsigned char target = 0xBD;
unsigned char small_value = 2;
unsigned char position = 0xC0; /* must be non-zero */
unsigned char temp = position;
int i = 0;

while (!(temp & 1))
{
temp >>= 1;
i++;
}
target &= ~position;
target |= (small_value << i);
printf("%#2X\n", target);
return 0;
}

--
Tim Hagan

Tim Hagan
Guest
Posts: n/a

 10-10-2003
Tim Hagan wrote:
>
> s wrote:
> >
> > If I have:
> >
> > unsigned char value = 0xBD;
> > unsigned char 2bits = 0x02;

>
> 2bits is an invalid identifier. The first character must be a letter or
> an underscore, followed by any sequence of digits and/or upper or lower
> case letters.

I tried to paraphrase K&R2 (a *big* mistake) and flubbed it. I should
have mentioned that underscores are considered to be letters and can
appear anywhere in identifier names. However, it is not recommended that
identifiers *begin* with an underscore since C library routines usually
use such names and conflicts may result. Also, some identifiers are
reserved as keywords (auto, break, case, char, ... ).

> > How do I set the two MSB in value to the two LSB in 2bits without
> > changing any other bits in value?
> >
> > <mytry>
> > value &= 0x3F; //set those two bits to zero
> > value |= (2bits<<6);
> > </mytry>

>
> Looks good to me. However, it isn't very interesting, since the two
> bits that you are trying to set in 'value' are already set you way
> want them.

Gak! I need a better proofreader.

> Observe:
>
> 0xBD = 10111101
> ^^
> 0x02 = 00000010
>
> > Do I have to clear(or set) those two bits first?

>
> You must clear them first, as you did in your example.

--
Tim Hagan