fork(1) download
  1. #include<iostream>
  2. const size_t LONG_SIZE = sizeof(long)*8; // size of a long type is 64 bits
  3.  
  4. using namespace std;
  5.  
  6. string b10_to_b2(long x)
  7. {
  8. string binNum;
  9. if(x < 0) // determine if the number is negative, a number in two's complement will be neg if its' first bit is zero.
  10. {
  11. binNum = "1";
  12. }
  13. else
  14. {
  15. binNum = "0";
  16. }
  17. int i = LONG_SIZE - 1;
  18. while(i > 0)
  19. {
  20. i --;
  21. if( (x & ( 1UL << i) ))
  22. {
  23. binNum = binNum + "1";
  24. }
  25. else
  26. {
  27. binNum = binNum + "0";
  28. }
  29. }
  30. return binNum;
  31. }
  32.  
  33. int main()
  34. {
  35. cout << b10_to_b2(-1) << endl;
  36. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
11111111111111111111111111111111