473,618 Members | 3,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bitset Macros

I found some bit set macros somewhere that look roughly like the following;

#define bitset_elem(ptr ,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr ,bit) (1 << (bit) % 8)

#define bitset_isset(pt r,bit) ((bitset_elem(p tr,bit) & bitset_mask(ptr ,bit)) != 0)
#define bitset_set(ptr, bit) (bitset_elem(pt r,bit) |= bitset_mask(ptr ,bit))
#define bitset_unset(pt r,bit) (bitset_elem(pt r,bit) &= ~bitset_mask(pt r,bit))
#define bitset_toggle(p tr,bit) (bitset_elem(pt r,bit) ^= bitset_mask(ptr ,bit))

Are these legal and can anyone recommend improvements?

Mike
Nov 14 '05 #1
13 8206
begin followup to Michael B Allen:
I found some bit set macros somewhere that look roughly like the
following;

#define bitset_elem(ptr ,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr ,bit) (1 << (bit) % 8)


One obvious issue is to use CHAR_BIT from limits.h instead of a hard
wired '8'.

--
Für Google, Tux und GPL!
Nov 14 '05 #2
"Alexander Bartolich" <al************ *****@gmx.at> wrote in message
news:bs******** ****@ID-193444.news.uni-berlin.de...
begin followup to Michael B Allen:
I found some bit set macros somewhere that look roughly like the
following;

#define bitset_elem(ptr ,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr ,bit) (1 << (bit) % 8)


One obvious issue is to use CHAR_BIT from limits.h instead of a hard
wired '8'.


The other would be to use 1u instead of 1, to cater for systems where
UCHAR_MAX > INT_MAX.

But I'd actually have to question what type ptr is and why the author feels
the need for the (unsigned char *) cast.

[Personally, I think the standard could help in these situations by defining
UINT_BIT and ULONG_BIT etc... to indiciate the number of (sign+) value bits
in a given integer type, not just unsigned char.]

--
Peter
Nov 14 '05 #3
On Wed, 31 Dec 2003 17:37:17 -0500, Alexander Bartolich wrote:
begin followup to Michael B Allen:
I found some bit set macros somewhere that look roughly like the
following;

#define bitset_elem(ptr ,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr ,bit) (1 << (bit) % 8)


One obvious issue is to use CHAR_BIT from limits.h instead of a hard
wired '8'.


Not obvious to me. Isn't char guaranteed to be 8 bits?

Mike
Nov 14 '05 #4
On Wed, 31 Dec 2003 20:15:50 -0500, Peter Nilsson wrote:
> I found some bit set macros somewhere that look roughly like the
> following;
>
> #define bitset_elem(ptr ,bit) ((unsigned char *)(ptr))[(bit)/8]
> #define bitset_mask(ptr ,bit) (1 << (bit) % 8)
<snip>
But I'd actually have to question what type ptr is and why the author
feels the need for the (unsigned char *) cast.


The type of 'ptr' can be a pointer to anything. For example it could be
an uint32_t * so that 32 bits can be tested with each iteration of
a loop when searching for the first free or used bit in a bit map.

Mike
Nov 14 '05 #5
Michael B Allen wrote:
On Wed, 31 Dec 2003 17:37:17 -0500, Alexander Bartolich wrote:

begin followup to Michael B Allen:
I found some bit set macros somewhere that look roughly like the
following;

#define bitset_elem(ptr ,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr ,bit) (1 << (bit) % 8)


One obvious issue is to use CHAR_BIT from limits.h instead of a hard
wired '8'.

Not obvious to me. Isn't char guaranteed to be 8 bits?

Nope. Just at *least* 8 bits

HTH,
--ag
--
--
Artie Gold -- Austin, Texas

Nov 14 '05 #6
"Michael B Allen" <mb*****@ioplex .com> wrote in message
news:pa******** *************** **********@iopl ex.com...
On Wed, 31 Dec 2003 20:15:50 -0500, Peter Nilsson wrote:
> I found some bit set macros somewhere that look roughly like the
> following;
>
> #define bitset_elem(ptr ,bit) ((unsigned char *)(ptr))[(bit)/8]
> #define bitset_mask(ptr ,bit) (1 << (bit) % 8)

<snip>

But I'd actually have to question what type ptr is and why the author
feels the need for the (unsigned char *) cast.


The type of 'ptr' can be a pointer to anything. For example it could be
an uint32_t * so that 32 bits can be tested with each iteration of
a loop when searching for the first free or used bit in a bit map.


That would introduce endian issues into your code.

If the type is actually irrelevant (only its size is important) then you
might as well simply change the declaration of the bit array object to an
array of unsigned char...

unsigned char bitfield[(32 + CHAR_BIT - 1)/ CHAR_BIT];

In anycase, using the above macros for this purpose would (in practice) be
highly inefficient. For the mask of the least significant bit you can simply
do...

uint32_t x = ...some value to be searched...
uint32_t m = x & -(x + 0u);

--
Peter
Nov 14 '05 #7
On Thu, 01 Jan 2004 00:12:44 -0500, Peter Nilsson wrote:
>> > #define bitset_elem(ptr ,bit) ((unsigned char *)(ptr))[(bit)/8]
>> > #define bitset_mask(ptr ,bit) (1 << (bit) % 8) <snip>
>
> But I'd actually have to question what type ptr is and why the author
> feels the need for the (unsigned char *) cast.


The type of 'ptr' can be a pointer to anything. For example it could be
an uint32_t * so that 32 bits can be tested with each iteration of a
loop when searching for the first free or used bit in a bit map.


That would introduce endian issues into your code.


Assuming different offsets within the bitset's memory aren't used and one
does not write the bitset memory to a file or the network, how exactly
are there "endian issues"?
If the type is actually irrelevant (only its size is important) then you
might as well simply change the declaration of the bit array object to
an array of unsigned char...

unsigned char bitfield[(32 + CHAR_BIT - 1)/ CHAR_BIT];
I suppose this is nice for determing the correct size based on the number
of bits but I don't see why I should define a bitset type when all you
need is a pointer to the target memory and maybe a pointer to the end
of the target memory.
In anycase, using the above macros for this purpose would (in practice)
be highly inefficient. For the mask of the least significant bit you can
simply do...

uint32_t x = ...some value to be searched...
uint32_t m = x & -(x + 0u);


So (1 << (bit) % 8) is "highly inefficient" because it doesn't use &
over % 1/8th of the time?

Mike
Nov 14 '05 #8
begin followup to Michael B Allen:
On Thu, 01 Jan 2004 00:12:44 -0500, Peter Nilsson wrote:
For the mask of the least significant bit you can simply do...

uint32_t x = ...some value to be searched...
uint32_t m = x & -(x + 0u);

If x == 1 then m = 1 & -1

http://www.duke.edu/~twf/cps104/twoscomp.html
# To get the two's complement negative notation of an integer, you
# write out the number in binary. You then invert the digits, and
# add one to the result.

Expressed as two's complement -1 means a word with all bits set.
m will then be 1, i.e. the same as x.
If x is a power of two, then m will always be equal to x.

What's the point?
So (1 << (bit) % 8) is "highly inefficient" because it doesn't
use & over % 1/8th of the time?

^^^^^^^^^^^^^^^ ^^
I don't understand this constraint. And I don't understand it's
relation to Nilsson's suggested improvement. Am I lame, or what?
Division is more expensive than bit manipulation. Always.
Given your original definitions:

#define bitset_elem(ptr ,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr ,bit) (1 << (bit) % 8)

(bit) / 8 is equivalent to (bit) >> 3.
(bit) % 8 is equivalent to (bit) & 7.

Both optimisations require that the constant operand is a power
of two, e.g. 8, 16 or 32.

You may rely on the compiler to optimize this out. In the end all
discussions of this kind break down to a disassembly listing dick
size war.

--
Für Google, Tux und GPL!
Nov 14 '05 #9
On Thu, 01 Jan 2004 09:07:31 -0500, Alexander Bartolich wrote:
For the mask of the least significant bit you can simply do...

uint32_t x = ...some value to be searched... uint32_t m = x & -(x +
0u);
So (1 << (bit) % 8) is "highly inefficient" because it doesn't use &
over % 1/8th of the time?

^^^^^^^^^^^^^^^ ^^
I don't understand this constraint. And I don't understand it's relation


I still don't understand. Is Peter talking about searching such that
you can obtain the lowest bit in a 32 bit portion of the bit set in one
step? That's nice but does this have anything to do with the bitset_elem
macro? If so, can someone please just post a version that demonstraits
the improvement? Benadryl and bit manipulation don't mix.
#define bitset_elem(ptr ,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr ,bit) (1 << (bit) % 8)

(bit) / 8 is equivalent to (bit) >> 3. (bit) % 8 is equivalent to (bit)
& 7.


Ok, so I can do:

#define bitset_mask(ptr ,bit) (1u << ((bit) & (CHAR_BIT - 1)))

but I don't know of a constant expression to get 3 from 8 or 4 from 16
or 5 from 32 assuming CHAR_BIT is a power of two so this doesn't help
with bitset_elem.

Mike

Nov 14 '05 #10

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

Similar topics

6
2318
by: Hunter Hou | last post by:
Hello, I have this very simple program, but it can't be compiled. What's wrong here? thanks, hunter #include <iostream>
2
5357
by: shaun roe | last post by:
As a follow up to my question about STL and bitset<64> I'd like to share a quirk (bug?) about unsigned long long support and the bitset. I'm using gcc 3.2 on linux or gcc 3.3 on mac, the answer is the same. unsigned long long int f; f=3; bitset<64> g(f); cout << f << endl; cout << g << endl; f<<=32;
5
5466
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): "...each character is 1 if the corresponding bit is set, and 0 if it is not. In general, character position i corresponds to bit position N - 1 -
5
4363
by: Rich S. | last post by:
Hi, Is the code below the best way to have the less than function for an 80 bit bitset or is there something faster / better? When I populate this map with millions (... and millions) of sets it gets slower and slower, I'm looking to make it faster. Thanks
14
3016
by: Haro Panosyan | last post by:
How to construct a bitset from string? For example: std::bitset<16> b16("1011011110001011"); Is this possible? Thanks in advance. -haro
3
2244
by: shaun | last post by:
I have a function for returning the value of a bit field in a number: template<typename T> T bitfield(const T num, const unsigned int bitStart, const unsigned int bitEnd){ T mask, shiftedNumber; unsigned int maskLength = bitEnd-bitStart+1; shiftedNumber = num>>bitStart; mask = (1<<maskLength)-1; return (shiftedNumber & mask);
4
2483
by: Sarath | last post by:
>From the documentation of MSDN, it is saying that bitset is not a STL container Unlike the similar vector<boolClass, the bitset class does not have iterators and is not an Standard Template Library container. Actaully what's so special in STL containers? What I know is that there will be certain operators overloaded, supports template meta programming, having iterators, compatible with
2
2717
by: arnuld | last post by:
i am confused on some aspects of bitset class: /* C++ Primer 4/e * chapter 3 * * exercise 3.23 * */ #include <iostream>
5
2332
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; // convert to bool reference& operator= ( bool x ); // assign from bool reference& operator= ( const reference& x ); // assign from bit reference& flip(); // flip bit value
0
8207
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
8650
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
8593
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
8453
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
6098
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
5552
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
4064
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...
1
1760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1455
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.