How to access predefined bit range in Bitset - container

Hello fellow programmers!

I´ve been struggling recently with accessing bit ranges in bitset-container (STL- Bitset container).
I have Bitset container which contains 64 bits (8 bytes) information.
With Bitset container´s own functions I can access to individual bits. However,
my goal is to create a function, where I give as argument some starting bit and ending bit in bitset container. Then the function returns the value, that those asked bits are holding.

float CalculateValue(int startingBit, int endingBit)
{
return ValueBetweenStartingBitAndEndingBit;
}

Could someone help me how to calculate the value of given range of bits in Bitset Container?
Thank you for any kind of help!
Last edited on
What exactly do you mean by value?
When bitset container contains these 64 bits, I want to think (just as a user) that it contains information as 8 bytes = 64 bits. Then I want to extract/copy some range of bits out of the container like here:


11110101 10101111 11100100 10101010 01001101 00101010 10101001 10101100

In this container is 64 bits and I want to extract the bolded bits and store them into some variable. Then my variable would contain number 10011 which would be the value of 19 in decimals.

I hope, this was helpful?
With java - language it´s already made:

"Function: get()

public BitSet get(int fromIndex, int toIndex)

Returns a new BitSet composed of bits from this BitSet from fromIndex (inclusive) to toIndex (exclusive)."

=> Then I would just return it as a unsigned long -type, like the bitset-class supports.



This is what I want, but unfortunately bitset class in C++ doesn´t support the get() - function :/
You can use member function to_ullong() to get the an unsigned long long value. Then you can use some bitwise operations to calculate the value you want.
You can use member function to_string and then extract from the result string required substring and apply string functions as for example stoi, stol, stof (if you need a float) and so on.
¿Is there a to_ullong() method?
You could
1
2
3
4
5
ulli value = 0;
for( size_t bit = start; bit<end; ++bit){
   value <<= 1;
   value or_eq Bit_Set[bit];
}


The problem is that the size of the bitset is a template parameter (needs to be know at compile time)
You could create your own bitset based on std::vector
Like ne555 said, there´s no to_ullong() method available in Bitset Container class.

There is just two different methods to get data out of the container:

to_ulong()
to_string()

All methods can be seen here:
http://www.cplusplus.com/reference/stl/bitset/

to_ulong() is not a proper one for my project because my data container contains 64 bits = 8 bytes which is more than unsigned long - type can handle. Unsigned long - type can take just 4 bytes.

That´s why I decided to use to_string() method. Situation is now this:

bitset<64> dataContainer;
bitset<64> mask;

// during the code the dataContainer is filled with data and mask is generated according to wanted bits

string sData;
string sMask;

sData = dataContainer.to_string();
sMask = mask.to_string();

// When I print the sData and sMask, they contain now:

sData = 0000000000000000011011100110010000110010000100101100000000000000
sMask = 0000000000000000000000000000000000000000001111111111000000000000

These values are totally right. So all I have to do is just use AND - operator to mask the data bits and then shift 12 times to right direction to get the wanted value.

Here are the instruction to use bitwise operators by Bitset class:
http://www.cplusplus.com/reference/stl/bitset/operators/

Then I just:

cout << (sData&sMask) << endl;

But I get this error:

error C2784: .... could not deduce template argument for ´const std::bitset<_Bits> & 'from 'std::string' // (four times)

error 2676: .... binary '&' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator


What is this all about? I do everything just like in instructions? Hope someone has experience of this. Thank you from previous help comments!
You are trying to use bitwise & on the the strings, which makes no sense. I think you meant to just use the bitwise & on the bitsets themselves:

cout << (dataContainer & mask) << endl;
There is no such an operator as operator & in class std:;string.

By the way std::bitset has member function to_ullong
to_ullong is new in C++11 so if you use an older compiler it might not work.

I was thinking of doing something like this:
1
2
3
4
unsigned long long CalculateValue(std::bitset<64>& bitset, int startingBit, int endingBit)
{
	return (bitset.to_ullong() >> startingBit) & ((1ULL << (endingBit - startingBit)) - 1);
}


Firedraco just opened my mind. I did exactly like he said. I used straight those Bitset Containers for "AND - operation" and it solved the problem. Then I just used bitshift operation "operator >>=" and printed out the value with to_ulong() - method.

Here´s the final function, if someone needs it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void calculateSignal(int start_bit, int end_bit, bitset<64> *dataContainer)  
{
	int maskLength = end_bit - start_bit+1; // Mask length from given Bit range
	int moveBitNr = start_bit;// value, how much we shift bits in the end of this function 
		
	bitset<64> mask;// creat mask, which will be used in &-operation
	for(int k=0; k<maskLength; k++)
	{
		mask.set(start_bit+k,1);
	}
	
	dataContainer->operator &=(mask);// masking &-operation

	dataContainer->operator >>=(moveBitNr);// shifting operation
	
	cout << "Wanted signal value is: " << dec << dataContainer->to_ulong() << endl;// print out as decimal number
}		



Thank you so much for everyone who used time to help me!
Topic archived. No new replies allowed.