Wednesday, May 23, 2012

How to convert integer number to a binary string in C++.

Converting integer to binary string is a very simple matter. for this there is a special class call "bitset".
so fallow the example bellow and try to play around "bitset"

For more information visit http://www.cplusplus.com/reference/stl/bitset/

#include <iostream>
#include <bitset>

void main(){

  //creating instance using bitset (16 bit). here you can specify the length suc as  8,16,32,64...
  std::bitset< 16 > btFlaged;

  //assigning integer values to instance
  btFlaged = 1234;

  //print bit string in the string
  for(int i =0; i< btFlaged.size(); i++){
    std::cout <<btFlaged.test(i) << std::endl;
  }
}



2 comments: