This function returns a binary representation in string form, of a given integer.
It seems to work fine when compiled;;;Code:#include <iostream> #include <string> using namespace std; string int2bin(int num) { string returnString; int pos=0; for(int x=0; x <= 15; x++) { pos = (pos == 0) ? 1 : pos + pos; returnString = (num & pos) ? "1"+returnString : "0"+returnString; } return returnString; } int main() { cout<<int2bin(255); return 0; }
I posted here in case you guys have any suggestions regarding better coding.