Casting a bitset to unsigned char, and vice versa

laurisvr

Working with unsigned char arrays, representing bits. I came across the following. On MSVC 2013 casting an std::bitset<8> to a char, and back. Seems to be a valid thing to do.

However in the C++11 ISO standard. I wasn't able to find a reference of this being valid. From what I have been able to gather, an std::bitset is merely is a bool array. With a more memory economic implementation and some functions surrounding it.

So in short, my question is: Is the code below valid.

unsigned char* myChar = new unsigned char(0x0F);
((std::bitset<8>*)myChar)->set(2);
((std::bitset<8>*)myChar)->reset(6);

std::cout << "expression result:" << (uint8_t)*myChar;
Barry

This is undefined behavior. The standard merely states that

The class template bitset<N> describes an object that can store a sequence consisting of a fixed number of bits, N.

It says nothing about the layout of this class internally. There is no guarantee that sizeof(bitset<8>) is 1. On my implementation, it happens to be 8. Thus, any assumption you are making about the internals of this class is just that - an assumption. If you want to convert an unsigned char to a bitset<8>, there is already an easy way to do that:

unsigned char myChar = 0x0F;
std::bitset<8> bs(myChar);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Casting Parameter Variables from int to char and vice-versa

C: Converting unsigned char array to signed int (vice versa)

How to convert from stringstream to unsigned char vector and vice versa?

Is converting from unsigned char to signed char and vice versa in C89 well defined?

BitSet to Set[Int] or vice versa in Scala

Casting Int as Unsigned Char

Int to UInt (and vice versa) bit casting in Swift

Problems casting a double into an unsigned char

Casting from `int` to `unsigned char`

ByteBuffer - convert long to char array and vice versa

Converting char to binary strings and vice-versa

Using a pointer for casting char* to unsigned char*

Casting from string to uint8 and vice versa

Type punning a positive signed integer into an unsigned (and vice versa)

Cast from unsigned long long to double and vice versa changes the value

Is casting from TYPE* to unsigned char* allowed?

c: type casting char values into unsigned short

Unsigned char array casting to long long

Java Char to its unicode hexadecimal string representation and vice-versa

Casting unsigned char to signed char for a red-black tree

Why does casting unsigned char* to char* require reinterpret_cast?

C# Signed & Unsigned Integral to Big Endian Byte Array, and vice versa using Bitwise way with "best" performance

convent a unsigned 32 bit integer to byte array which length is 4 byte and vice versa in java

How to derive signed type from unsigned type generic parameter, and vice-versa?

casting unsigned char * buffer to a callable void pointer that can take an argument

byte to string and vice versa

XML To JSON and Vice Versa

IA32 Assembly code for type casting from signed/unsigned char to unsigned/signed int

SAS: What is the quickest way to convert a dataset compressed in binary to compressed using char or vice versa?

TOP Ranking

HotTag

Archive