473,657 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with BitSet.length() ...

.... is that it does not return the length of the bit set. For example, if
I create a new 8-bit set:

BitSet eightBits = new BitSet(8);
Running eightBits.lengt h() returns zero, rather than eight.

The problem is that I want to be able to convert the bit set back to an
integer at some point. I thought the easiest way would be to iterate
backwards over the bit set thus:

private static int bitsToInt(BitSe t bits) {
int myInt = 0;
int binColumn = 1;

// loop over the bitset backwards, adding up the true values
for (int i=(bits.length( )-1); i>=0; i--) {
myInt += (booleanToInt(b its.get(i)) * binColumn);
binColumn = (binColumn * 2);
}

return myInt;
}

But because BitSet.length only returns the length from the highest
bit that's set to "true", this doesn't work for any binary numbers that
start with zeros in the full bit set (e.g. an 8-bit set of '00111000').

I suppose my method could take in a 'length' parameter that I coul define,
but isn't there a neater way of getting the true length of a BitSet?
Aug 16 '07 #1
1 6012
Mothra wrote:
Running eightBits.lengt h() returns zero, rather than eight.
Logically speaking, if length() returns the perceived size and not the
actual size, then there would be a function that returns the actual size
if it might be pertinent. This function would also have a name similar
to "length", like "capacity" or "size". Capacity is used for both
ArrayList and String{Builder, Buffer}, for example. Searching the
Javadocs for BitSet reveals this (in the function summary table):

size() - Returns the number of bits of space actually in use by this
BitSet to represent bit values.
private static int bitsToInt(BitSe t bits) {
What if BitSet.size() 32?
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Aug 16 '07 #2

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

Similar topics

6
2320
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
5365
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;
2
7303
by: Michal Wyrebski | last post by:
Hello, I was thinking how is the fastest and the best way of passing bitset variable to some function. Only one method came to my mind. It's about passing converted value: #v+ void func(unsigned long UL) { bitset<8> B(UL); cout << "B: " << B << endl;
14
3018
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
11
2103
by: felixnielsen | last post by:
What i have is not even a real problem, but i hope someone can help anyway, but first a piece of code_ @code start const unsigned short Size = 2; // 2^N std::bitset<Size*Size*Size> YZ; std::bitset<Size*Size*Size> XZ; std::bitset<Size*Size*Size> XY; std::bitset<Size*Size*Size> V; union {
1
1485
by: sadvipra | last post by:
Hi, I am a bioengineer new to C++ (and new to programing in general). I need to create a bit array for an array of integer, where each element of the bit array would be a N-bit representation of each element of the array of integer. My first inclination was to use bitset and utilize the bit manipulation operations it provided, however looks like it is not possible to construct a bitset without explicitly providing the argument, e.g int...
4
2484
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
2334
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
8420
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
8324
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
8842
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
8740
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
8516
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,...
0
8617
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
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2743
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
1733
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.