473,699 Members | 3,239 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 6533
"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
2451
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
5700
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
4803
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
3888
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
32295
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
4830
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
14660
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
4257
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
1852
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
2344
by: spasmous | last post by:
Just wondering.
0
8706
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
9199
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
9055
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...
1
8947
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6552
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...
0
4392
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3076
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
2366
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.