473,772 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set/Unset Bits Operation

I've written a small operation that sets/unsets bits within an
unsigned integer. Is there a better way to do this?

static uint setBits(uint original, uint newValue, uint offset, unint
mask)
{
return ((original >offset) & (~mask) | newValue) << offset;
}

Example:

uint test = 0xEC; // 1110 1100
uint TwoBitMask = 0x03;

uint offset = 3;

// Expect 1110 0100
uint result = setBits(test, 0, offset, TwoBitMask);

// Expect 1111 1100
result = setBits(test, 3, offset, TwoBitMask);

// Expect 1111 0100
result = setBits(test, 2, offset, TwoBitMask);

Jul 6 '07 #1
2 3372
Small bug in that last one (bits dropped when shifted). Let's try
that again:

static uint setBits(uint original, uint newValue, uint offset, uint
mask)
{
return original & ~(mask << offset) | (newValue << offset);
}

On Jul 6, 11:07 am, "O.B." <funkj...@bells outh.netwrote:
I've written a small operation that sets/unsets bits within an
unsigned integer. Is there a better way to do this?

static uint setBits(uint original, uint newValue, uint offset, unint
mask)
{
return ((original >offset) & (~mask) | newValue) << offset;

}

Example:

uint test = 0xEC; // 1110 1100
uint TwoBitMask = 0x03;

uint offset = 3;

// Expect 1110 0100
uint result = setBits(test, 0, offset, TwoBitMask);

// Expect 1111 1100
result = setBits(test, 3, offset, TwoBitMask);

// Expect 1111 0100
result = setBits(test, 2, offset, TwoBitMask);

Jul 6 '07 #2
O.B. wrote:
Small bug in that last one (bits dropped when shifted). Let's try
that again:

static uint setBits(uint original, uint newValue, uint offset, uint
mask)
{
return original & ~(mask << offset) | (newValue << offset);
}
I was just thinking when reading the first post that you should shift
the mask and the value instead of shifting the original. :)

--
Göran Andersson
_____
http://www.guffa.com
Jul 6 '07 #3

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

Similar topics

7
2132
by: lawrence | last post by:
Given a bunch of mixes variables, I'm in a situation where I can't know what type they are. Can I use unset to kill an object? unset($object); Can I use unset to kill a pointer to an open file? unset($fp); Can I use unset to kill a pointer to a database return? unset($mySqlResult);
3
14906
by: Phil Powell | last post by:
PHP: unset($_SESSION); In my original environment (PHP 4.3.2) this line will delete the session variable 'mainDisplay'. But in the testing environment (PHP 4.3.6) the variable persists even after unset() is called. Is there some special setting in PHP that can allow a session to
21
3394
by: steve | last post by:
Dont’ make my mistake. It is costly. Say you have defined a variable $var in your main script. Now in a function you access it using: global $var; But you want to set it to null inside the function. DO: $var = null; DONT DO: unset($var);
1
1892
by: frizzle | last post by:
Hi group, Why won't $new_var be unset in the following function? Am i missing out something? Greetings Frizzle. *************************************
53
8218
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is all-bits-zero, and does not therefore guarantee useful null pointer values (see section 5 of this list) or floating-point zero values.
7
1861
by: fasanay | last post by:
Hi everybody I have got the following PHP code which I am trying to convert to ASP any help will be appreciated...I have done most of it but I cant find a replace function for Unset in asp which will discard the variable alltogether... if ($categoryid == "all") { $sql = "SELECT * FROM products where shopinspection=$shopinspection"; unset($HTTP_POST_VARS); unset($HTTP_POST_VARS);
5
2298
by: comp.lang.php | last post by:
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA $_POST if ($_POST && (!is_array($leaseObj->errorArray) || @sizeof($leaseObj->errorArray) == 0)) { print_r(array_keys($_POST)); @reset($_POST); $tempPost = $_POST; foreach ($_POST as $key =$val) if (strpos($key, 'new_') === 0) array_remove($tempPost, $tempPost);
19
2775
by: sarahh | last post by:
Hi, I need help in the following question . I have a cpu that knows to do the computations on 32 bits(unsigned integer( write a function that gets 2 64 bits numbers and return their sum. I start with the following structure. typedef struct{ unsigned int low; unsigned int high; }64bits;
0
1417
Airslash
by: Airslash | last post by:
Hello, I've written a small function to delete a variable from a class' internal array. The variables on their own are custom class objects, and I'm a bit confused about the whole pass by reference thing. First I'll show you the code: # removes a parameter from the array. # variables are passed as reference to prevent overhead.
0
9621
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
9454
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
10264
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
10106
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
9914
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...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
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
3
2851
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.