473,750 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6535
"s" <youshouldbe@ho me> 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
2458
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 have a type, one of: unsigned long signed long float double
40
5711
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 welcome any improvements: i = 0; if (g && 1) i++; if (g && 2) i++;
7
4805
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
3894
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 OpenSSL's sha1() to convert the string to a SHA-1 160-bit message digest. The question is: how do I use these 160 bits to get my final 32 bits? Should I use the first 32 bits or the last 32 bits or is there yet another method? Note: I understand...
5
32300
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 & (byte)(1 << pos)) != 0); }
15
4848
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
14664
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
4275
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 must be at least 8 bits: "The values given below shall be replaced by constant expressions suitable for use in #if
11
1857
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 = binTemp * 128 + binTemp * 64 + binTemp * 32 + binTemp * 16 + binTemp * 8 + binTemp * 4 + binTemp * 2 + binTemp; But I want to populate that same pad from bits:
11
2349
by: spasmous | last post by:
Just wondering.
0
8999
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9394
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6803
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.