Connecting Tech Pros Worldwide Forums | Help | Site Map

Set bits

s
Guest
 
Posts: n/a
#1: Nov 13 '05
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
#2: Nov 13 '05

re: Set bits


"s" <youshouldbe@home> schrieb im Newsbeitrag
news:<3F85BE28.1020901@home>...
[color=blue]
> If I have:[/color]
[color=blue]
>[/color]
[color=blue]
> unsigned char value = 0xBD;[/color]
[color=blue]
> unsigned char 2bits = 0x02;[/color]
[color=blue]
>[/color]

Your identifiert may not start with a number. You may use two_bits instead.
[color=blue]
> How do I set the two MSB in value to the two LSB in 2bits without[/color]
[color=blue]
> changing any other bits in value?[/color]
[color=blue]
>[/color]
[color=blue]
> <mytry>[/color]
[color=blue]
> value &= 0x3F; //set those two bits to zero[/color]
[color=blue]
> value |= (2bits<<6);[/color]
[color=blue]
> </mytry>[/color]

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

Kind regards,


s
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Set bits


Thanks for the reply and sorry for the bad identifier!

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:[color=blue]
> Your identifiert may not start with a number. You may use two_bits instead.[color=green]
>>value |= (2bits<<6);[/color]
> Use the now identifier in your try and it should work.
>
> Kind regards,
>
>[/color]

Tim Hagan
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Set bits


s wrote:[color=blue]
>
> If I have:
>
> unsigned char value = 0xBD;
> unsigned char 2bits = 0x02;[/color]

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.
[color=blue]
> 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>[/color]

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
[color=blue]
> Do I have to clear(or set) those two bits first?[/color]

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

--
Tim Hagan
Tim Hagan
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Set bits


s wrote:[color=blue]
>
> 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.[/color]

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
#6: Nov 13 '05

re: Set bits


Tim Hagan wrote:[color=blue]
>
> s wrote:[color=green]
> >
> > If I have:
> >
> > unsigned char value = 0xBD;
> > unsigned char 2bits = 0x02;[/color]
>
> 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.[/color]

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, ... ).
[color=blue][color=green]
> > 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>[/color]
>
> 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.[/color]

Gak! I need a better proofreader.
[color=blue]
> Observe:
>
> 0xBD = 10111101
> ^^
> 0x02 = 00000010
>[color=green]
> > Do I have to clear(or set) those two bits first?[/color]
>
> You must clear them first, as you did in your example.[/color]

--
Tim Hagan
Closed Thread