Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. how do I improve: The number to binary code C++

how do I improve: The number to binary code C++

Scheduled Pinned Locked Moved Clever Code
c++tutorialquestioncode-reviewlearning
3 Posts 3 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Minwoo Ju
    wrote on last edited by
    #1

    #include
    #include
    using namespace std;

    void line() { cout << setw(12) << setfill('-') << endl;}

    int main() {

    int binary[8];
    int num;
    int bits[8] = {128,64,32,16,8,4,2,1};

    do {

    line();
    cout << "write in the number to be converted into binary." << endl;
    cin >> num;

    for(int i = 0;i < 8;i++) {

    if(num >= bits[i]){

    binary[i] = true;
    num = num - bits[i];

    }

    else if(num < bits[i]) {

    binary[i] = false;
    }

    }

    for(int i = 0;i < 8;i++) {

    cout << binary[i] ;

    }
    }while (1);

    system("pause");
    return 0;
    }

    this code is made about 1 month ago,and this is supposed to convert numbers into binary. Since I'm a beginner,the code might be messy and unintelligent. Please tell me how to improve this or to make it more efficient. If by any chance I'm in the wrong section, forgive me,this is the first time visiting this site.

    OriginalGriffO 1 Reply Last reply
    0
    • M Minwoo Ju

      #include
      #include
      using namespace std;

      void line() { cout << setw(12) << setfill('-') << endl;}

      int main() {

      int binary[8];
      int num;
      int bits[8] = {128,64,32,16,8,4,2,1};

      do {

      line();
      cout << "write in the number to be converted into binary." << endl;
      cin >> num;

      for(int i = 0;i < 8;i++) {

      if(num >= bits[i]){

      binary[i] = true;
      num = num - bits[i];

      }

      else if(num < bits[i]) {

      binary[i] = false;
      }

      }

      for(int i = 0;i < 8;i++) {

      cout << binary[i] ;

      }
      }while (1);

      system("pause");
      return 0;
      }

      this code is made about 1 month ago,and this is supposed to convert numbers into binary. Since I'm a beginner,the code might be messy and unintelligent. Please tell me how to improve this or to make it more efficient. If by any chance I'm in the wrong section, forgive me,this is the first time visiting this site.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      It is the wrong forum - you want either Quick Answers[^] or tHE c++ Forum[^] However, that is a bit of an odd way to do it. I would have a single loop and a single test:

      for (int i = 0; i < 8; i++)
      {
      cout << (num & 0x80) != 0 ? "1" : "0";
      num <<= 1;
      }

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      R 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        It is the wrong forum - you want either Quick Answers[^] or tHE c++ Forum[^] However, that is a bit of an odd way to do it. I would have a single loop and a single test:

        for (int i = 0; i < 8; i++)
        {
        cout << (num & 0x80) != 0 ? "1" : "0";
        num <<= 1;
        }

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        R Offline
        R Offline
        robocodeboy
        wrote on last edited by
        #3

        Or (untested):

        for(int i = 0x80; i > 0; i >>= 1)
        {
        cout << (num & i) ? "1" : "0";
        }

        Dunno if the code spared is worth it, though.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups