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 "00110000111110 00"
=> 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 5 5473
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 "00110000111110 00"
=> 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
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 "00110000111110 00"
=> 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 "00110000111110 00". 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
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_ul ong ());
high = static_cast <unsigned char> (b8_higher.to_u long ());
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?
In article <1e************ *************** ***@40tude.net> ,
SpOiLeR <request@no_spa m.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_ul ong ()); high = static_cast <unsigned char> (b8_higher.to_u long ());
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';
}
001011000100100 0
000100100011010 0
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';
001011000100100 0
-Howard
On Wed, 16 Mar 2005 02:04:46 GMT, Howard Hinnant wrote: In article <1e************ *************** ***@40tude.net> , SpOiLeR <request@no_spa m.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_ul ong ()); high = static_cast <unsigned char> (b8_higher.to_u long ());
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'; }
001011000100100 0 000100100011010 0
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';
001011000100100 0
-Howard
Well, considering all this, std::bitset is one really nice object :)!!!
Thanks everybody for your help... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 order.
|
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?
|
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
|
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;
} v;
|
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 can not use std::bitset in an iterator. Any help apreciated.
Cheers
Sean.
| |
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 implement efficiently (for bitsets too large
for to_ulong) outside of the class.
For now I will use boost::dynamic_bitset, which does implement it.
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| | |