473,246 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

std::bitset, standard and endianess

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 -
i..."

On my machine, it results in most significant bits being on lower positions
in resulting string:

unsigned int i = 12536;
std::bitset<16> bs = i;
std::string str = bs.to_string();

which gives for str "0011000011111000"

=> 0011 0000 1111 1000 == x30F8 == 12536

If I understand my docs well, on machine with different endianess, same
code will result in different string. What does standard say about it: will
string output always have most significant bits on lowest string positions,
or it is like my docs say?

TIA
Jul 23 '05 #1
5 5328
SpOiLeR wrote:
q1: Is std::bitset<N> part of standard or it's compiler extension?
Standard.
q2: Is std::bitset::to_string() part of standard?
Yes.
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 -
i..."

On my machine, it results in most significant bits being on lower positions
in resulting string:

unsigned int i = 12536;
std::bitset<16> bs = i;
std::string str = bs.to_string();

which gives for str "0011000011111000"

=> 0011 0000 1111 1000 == x30F8 == 12536

If I understand my docs well, on machine with different endianess, same
code will result in different string.
Your understanding is wrong.
What does standard say about it: will
string output always have most significant bits on lowest string positions,
or it is like my docs say?


The Standard does not concern itself with endianness. So, yes, the most
significant bit will the at the beginning of the resulting string.

V
Jul 23 '05 #2
SpOiLeR wrote:
...
q1: Is std::bitset<N> part of standard or it's compiler extension?
It is a part of the standard library.
q2: Is std::bitset::to_string() part of standard?
Yes.
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 -
i..."

On my machine, it results in most significant bits being on lower positions
in resulting string:

unsigned int i = 12536;
std::bitset<16> bs = i;
std::string str = bs.to_string();

which gives for str "0011000011111000"

=> 0011 0000 1111 1000 == x30F8 == 12536

If I understand my docs well, on machine with different endianess, same
code will result in different string.
No. The resultant string will contain the binary representation of
number 12536 (decimal), possibly with extra leading zeros. In binary
representation 12536 is "0011000011111000". It doesn't depend on the
endianness of the hardware platform. There's nothing in the
specification of 'std::bitset<>' that depends on the endianness of the
hardware platform in any way.
What does standard say about it: will
string output always have most significant bits on lowest string positions,
Yes.
or it is like my docs say?


That's actually exactly what your docs say. You just have to interpret
them at higher (logical) level.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #3
On Mon, 14 Mar 2005 16:09:05 -0800, Andrey Tarasevich wrote:
There's nothing in the
specification of 'std::bitset<>' that depends on the endianness of the
hardware platform in any way.


So, if I write something like this:

std::bitset<16> b16 (12345); // 12345 can fit in 16 bits
std::bitset<8> b8_higher, b8_lower;

unsigned int i;
for (i=0; i<8; i++) b8_lower[i] = b16[i];
for (i=8; i<16; i++) b8_higher[i-8] = b16[i];

unsigned char low, high;

// Casts OK because standard guarantees that char contains at least 8 bits
low = static_cast <unsigned char> (b8_lower.to_ulong ());
high = static_cast <unsigned char> (b8_higher.to_ulong ());

Is it guaranteed that low will contain less significant bits of number
contained in b16, and higher will contain more significant bits of that
number?
Jul 23 '05 #4
In article <1e******************************@40tude.net>,
SpOiLeR <request@no_spam.org> wrote:
On Mon, 14 Mar 2005 16:09:05 -0800, Andrey Tarasevich wrote:
There's nothing in the
specification of 'std::bitset<>' that depends on the endianness of the
hardware platform in any way.
So, if I write something like this:

std::bitset<16> b16 (12345); // 12345 can fit in 16 bits
std::bitset<8> b8_higher, b8_lower;

unsigned int i;
for (i=0; i<8; i++) b8_lower[i] = b16[i];
for (i=8; i<16; i++) b8_higher[i-8] = b16[i];

unsigned char low, high;

// Casts OK because standard guarantees that char contains at least 8 bits
low = static_cast <unsigned char> (b8_lower.to_ulong ());
high = static_cast <unsigned char> (b8_higher.to_ulong ());

Is it guaranteed that low will contain less significant bits of number
contained in b16, and higher will contain more significant bits of that
number?


Yes. The standard mandates that the bits will appear to be stored in
little endian order as observed by the indexing operator:

23.3.5p3:
When converting between an object of class bitset<N> and a value of some
integral type, bit position pos corresponds to the bit value 1 << pos .
However, just in case we caught you napping, to_string is guaranteed to
present the bits in big endian order: ;-)

23.3.5.2p19 (describing to_string):
Character position N - 1 corresponds to bit position zero. Subsequent
decreasing character positions correspond to increasing bit positions.


#include <bitset>
#include <iostream>

int main()
{
std::bitset<16> bs(0x1234);
for (unsigned i = 0; i < 16; ++i)
std::cout << (bs[i] ? 1: 0);
std::cout << '\n';
std::string srep = bs.to_string();
std::cout << srep << '\n';
}

0010110001001000
0001001000110100

Fortunately bitsets constructed from strings are consistent with
to_string and interpret the string in big endian order.

std::bitset<16> bs2(srep);
for (unsigned i = 0; i < 16; ++i)
std::cout << (bs2[i] ? 1: 0);
std::cout << '\n';

0010110001001000

-Howard
Jul 23 '05 #5
On Wed, 16 Mar 2005 02:04:46 GMT, Howard Hinnant wrote:
In article <1e******************************@40tude.net>,
SpOiLeR <request@no_spam.org> wrote:
On Mon, 14 Mar 2005 16:09:05 -0800, Andrey Tarasevich wrote:
There's nothing in the
specification of 'std::bitset<>' that depends on the endianness of the
hardware platform in any way.
So, if I write something like this:

std::bitset<16> b16 (12345); // 12345 can fit in 16 bits
std::bitset<8> b8_higher, b8_lower;

unsigned int i;
for (i=0; i<8; i++) b8_lower[i] = b16[i];
for (i=8; i<16; i++) b8_higher[i-8] = b16[i];

unsigned char low, high;

// Casts OK because standard guarantees that char contains at least 8 bits
low = static_cast <unsigned char> (b8_lower.to_ulong ());
high = static_cast <unsigned char> (b8_higher.to_ulong ());

Is it guaranteed that low will contain less significant bits of number
contained in b16, and higher will contain more significant bits of that
number?


Yes. The standard mandates that the bits will appear to be stored in
little endian order as observed by the indexing operator:

23.3.5p3:

When converting between an object of class bitset<N> and a value of some
integral type, bit position pos corresponds to the bit value 1 << pos .


Excellent!
However, just in case we caught you napping, to_string is guaranteed to
present the bits in big endian order: ;-)

23.3.5.2p19 (describing to_string):
Character position N - 1 corresponds to bit position zero. Subsequent
decreasing character positions correspond to increasing bit positions.


#include <bitset>
#include <iostream>

int main()
{
std::bitset<16> bs(0x1234);
for (unsigned i = 0; i < 16; ++i)
std::cout << (bs[i] ? 1: 0);
std::cout << '\n';
std::string srep = bs.to_string();
std::cout << srep << '\n';
}

0010110001001000
0001001000110100

Fortunately bitsets constructed from strings are consistent with
to_string and interpret the string in big endian order.

std::bitset<16> bs2(srep);
for (unsigned i = 0; i < 16; ++i)
std::cout << (bs2[i] ? 1: 0);
std::cout << '\n';

0010110001001000

-Howard


Well, considering all this, std::bitset is one really nice object :)!!!
Thanks everybody for your help...
Jul 23 '05 #6

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

Similar topics

2
by: Dill Hole | last post by:
Can anyone tell me why std::bitset<2> foo(std::string("01")) initializes the bitset in reverse order, i.e. foo=true foo=false I would expect the bitset to be initialized in string text...
3
by: Gaijinco | last post by:
In C++ this code: for(int i=0; i<16; ++i){ std::cout << std::bitset<4>(i) << std::endl; Will print numbers 1-15 as binaries with 4 bits. Is there any equivalent of bitset in C?
14
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
7
by: felixnielsen | last post by:
I would wery much like this to work: @code start #include <iostream> #include <bitset> const unsigned short size = 2; // 2^<unsigned int> // union V { char c; std::bitset<size*size*size> b;...
5
by: Sean Farrow | last post by:
Hi: I have an iterator defined as follows: std::map<std::bitset<6>, int>::iterator DotsIterator, SignsIterator; I get errors that std::bitset does not declare the < operator. Does this mean I...
3
by: Guy.Tristram | last post by:
Is there any good reason operator< is not defined for std::bitset? It seems to me that: 1 - it would be useful. 2 - it is easy to implement inside the class template. 3 - it is impossible to...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.