![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
This bit flags class is a pretty handy utility that could be needed from time to time. It's small and it saves a lot of typing. It strictly uses integer types ( or I suppose types that are copy constructable and provide operators >>, <<, |, &, ^, ~, != and ==). The nce thing about this bit class is that you can do stuff like
bits[0] = false which operate on individual bits by having operator [] return a type of proxy object that is technically an intermediary between operator [] and the assignment of true/false to a single bit. Code:
|
|
|
|
|
|
#2 |
|
DevMaster Staff
Join Date: Jan 2003
Posts: 1,201
|
Awsome work! This can prove to be very useful for a recent thing I was working on
![]() I just tested it, and it compiles and works like a charm. |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jan 2003
Posts: 868
|
I'm sure that thing will be useful to the people that are used to all that C++ mess but i'll have to port it to Java, C or Pascal to make it useful to me!
|
|
|
|
|
|
#4 |
|
DevMaster Staff
Join Date: Jan 2003
Posts: 1,201
|
If you're talking about templates, then C# and Java support "generics", which is supposed to be similar to C++ templates.
|
|
|
|
|
|
#5 |
|
Valued Member
Join Date: Aug 2004
Posts: 120
|
Alternatively, you could use an array rather than templates.
Java users: without operator overloading you should just drop the "proxy" concept because you need to rely on setBit and getBit There might even be a built in type for this. C# users: just use System.Collections.BitArray |
|
|
|
|
|
#6 |
|
Member
Join Date: Oct 2005
Posts: 54
|
What in the world is this? If you're trying to get a bit from a byte or something it can be coded in less than 20 lines. What is a "bit size"? Last time i checked a bitsize is an intrinsic, 1/8 of a byte.
|
|
|
|
|
|
#7 |
|
DevMaster Staff
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
|
If you want to get a bit from a byte you can do it in one line. Bladder's contribution is a class that wraps those accesses, making them much more readable, i.e. by writing bits[i] = false instead of bits &= ~(1 << i).
That said, I believe most implementations of the STL have a specialization for std::vector<bool> that does basically the same thing as this, although it doesn't have all the bells and whistles like providing bitwise operators that work on the whole array at once.
___________________________________________
Currently working at Sucker Punch reedbeta.com - OpenGL demos and other projects Luabridge - a lightweight, dependency-free C++/Lua binding library. CD Lite - an unobtrusive, minimal CD player application for Windows. |
|
|
|
|
|
#8 |
|
Member
Join Date: Oct 2005
Posts: 54
|
Yeah well its still a convoulted mess. templated bit accessor makes no sense to me. I wrote a class like that in literally 20 lines and replaced a template with a ptr to unsigned char. Granted I didn't implement the bitwise overloads but that is extremely straightforward and shouldnt have made the code over 100 lines. Then all you need to do is i/8 to get the byte and i % 8 to get the bit and you can even get rid of the / and % if you don't like it and it works for arrays not just user-defined types.
Also if he wanted to make it a template he should have a template parameter indicating which end of the bit he wants to get. For ex, for jpeg decompression the first bit is 1<<i in the huffman decoding phase but for windows monochrome bitmaps the top left pixel is 128>>i of the first byte. |
|
|
|
|
|
#9 |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
What's wrong with std::bitset?
also, there are CHAR_BIT bits in a char (byte), which doesn't need to be 8 ![]()
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. Last edited by .oisyn : 11-15-2005 at 06:12 AM. |
|
|
|
|
|
#10 | ||
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
CobraLionz: It's easier to save to and load from files with this thing. And it saves a lot of unnecessary typing and increases readability. It's faster then using division and %, and sometimes 32 bits is either unnecessary (char*) or is not enough. And bit access is standard, bit[0] is the least significant and bit[bit.size()-1] is the most significant. I havent found any need to add a second template parameter.
Quote:
Good question, apparently nothing form what i've just read about it . Never thought of looking for something like this in std because I just assumed something like this didn't exist. All it doesnt have is that "extract" function.Quote:
I always thought that a "char" data type was fixed to 8, but the short/int/long type sizes could vary. But ok. |
||
|
|
|
|
|
#11 |
|
Senior Member
Join Date: Oct 2005
Location: Pensacola, FL
Posts: 1,028
|
I'm writing two data link programs right now talking to search radars with 9 and 13-bit word lengths. There's a whole lot of bit shifting in my future!
|
|
|
|
|
|
#12 | |
|
Senior Member
Join Date: Aug 2004
Location: USA
Posts: 829
|
Quote:
http://java.sun.com/j2se/1.5.0/docs/...il/BitSet.html You're welcome ![]()
___________________________________________
Jesse Coyle |
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|