473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Set bits

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?

Nov 13 '05 #1
5 6512
"s" <youshouldbe@home> schrieb im Newsbeitrag
news:<3F85BE28.1020901@home>...
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? <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,
Nov 13 '05 #2
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

Frank Roland wrote:
Your identifiert may not start with a number. You may use two_bits instead.
value |= (2bits<<6);

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

Kind regards,


Nov 13 '05 #3
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
Nov 13 '05 #4
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
Nov 13 '05 #5
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
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Ben | last post by:
Hi, I need to write some data types into an array of unsigned chars. These are basically "signals" within a message, so each signal will have a start bit and a length. The signals will also...
40
by: aku | last post by:
I'm looking for the absolute fastest way to count the nr of bits that are set to "1" in a string. Presumably I then first need the fastest way to do this in a byte. I think this is it, but...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
6
by: barcaroller | last post by:
I couldn't find a message-digest newsgroup, so I posted here. I have a C function that converts a string of arbitrary length to a 32-bit hash value. I realize this is overkill but I used...
5
by: Oyvind Eriksen | last post by:
Hello. I need to read bits from bytes in a file. I have code that works but it's very slow. Can anybody help me? The code I have is: private bool GetBit(byte b, int pos) { return ((b &...
15
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
11
by: Mack | last post by:
Hi all, I want to write a program to count number of bits set in a number. The condition is we should not loop through each bit to find whether its set or not. Thanks in advance, -Mukesh
77
by: borophyll | last post by:
As I read it, C99 states that a byte is an: "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment" (3.6) and that a byte...
11
by: JoeC | last post by:
I am working on a graphics program but my question has nothing to do with graphics but trying to get an algorithm to work. I set graphics from a 16x16 grid to bits of a graphic with: bitData =...
11
by: spasmous | last post by:
Just wondering.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.