Set bits

S

s

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

Frank Roland

If I have:

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

Your identifiert may not start with a number. You may use two_bits instead.
How do I set the two MSB in value to the two LSB in 2bits without
changing any other bits in value?


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

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

Kind regards,
 
S

s

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
 
T

Tim Hagan

s said:
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.
 
T

Tim Hagan

s said:
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;
}
 
T

Tim Hagan

Tim said:
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, ... ).
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.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top