Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i convert bitset into short in c++?

if i have a bitset<16> bits(*iter) and a my short how i can assign this bist to my short?

short myShort = ??bits??

It's possible to convert a bitset<16> to short?

like image 241
Safari Avatar asked Nov 17 '11 10:11

Safari


2 Answers

You really should use an unsigned short, to avoid language quirks on the high bit.

unsigned short myShort = (unsigned short)bits.to_ulong();
like image 56
Marcelo Cantos Avatar answered Sep 20 '22 02:09

Marcelo Cantos


I'd use the to_ulong method for this, and cast the result (since you know that only the lowest 16 bit will be used):

short myShort = static_cast<short>( bits.to_ulong() );
like image 44
Frerich Raabe Avatar answered Sep 22 '22 02:09

Frerich Raabe