jeudi 30 juin 2016

Quickly converting a string in the format of '0 1 1 0 1' into a bitset

How can I quickly convert a string of ones and zeroes separated by spaces into a bitset?

There exists a constructor to initialize a bitset from a string not separated by spaces, one to initialize a bitset to all zeroes or ones, and one to initialize from an integer. Off the top of my head, I can think of three ways:

  • Removing the spaces from the string and passing it to the constructor
  • Converting the binary into an integer and passing it to the constructor
  • Initializing all values to zero and changing the value of each bit according to the string in a for-loop

The number of bits is 24, and each string has exactly 24 bits, no more, no less.

Aucun commentaire:

Enregistrer un commentaire