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. General Programming
  3. C / C++ / MFC
  4. reverse division to convert decimals to dinary

reverse division to convert decimals to dinary

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelptutorialquestion
11 Posts 3 Posters 0 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.
  • O Offline
    O Offline
    Omegaclass
    wrote on last edited by
    #1

    hi everyone who reads this, i am in a digital electronics class and i decided to write a program that converts decimals to binary, i thought this was going to be simple and it was till i realized my my loop cout'ed the binary in reverse order. 731 in dec = 1011011011 and my program did it in reverse 1101101101 i used int's with modulus and division operators:

    while (num)
    {
    rem=num%2;
    num = num/2;
    cout << rem;

    }

    so i was wondering is there a simple way to reverse the output or could the division be done in reverse so the last output is first and the first last in my program? please note i am a beginner at c++ so i don't know how to do arrays as of yet and i am sure the output could be loaded into an array and printed in reverse order. thank you in advanced for any help.

    CPalliniC D 2 Replies Last reply
    0
    • O Omegaclass

      hi everyone who reads this, i am in a digital electronics class and i decided to write a program that converts decimals to binary, i thought this was going to be simple and it was till i realized my my loop cout'ed the binary in reverse order. 731 in dec = 1011011011 and my program did it in reverse 1101101101 i used int's with modulus and division operators:

      while (num)
      {
      rem=num%2;
      num = num/2;
      cout << rem;

      }

      so i was wondering is there a simple way to reverse the output or could the division be done in reverse so the last output is first and the first last in my program? please note i am a beginner at c++ so i don't know how to do arrays as of yet and i am sure the output could be loaded into an array and printed in reverse order. thank you in advanced for any help.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      You should go in the opposite way, using multiplication (or bit shif, that is faster):

      #include <iostream>
      using namespace std;
      void main()
      {
      unsigned int k = 731;
      unsigned int b;
      const int SIZE = sizeof(k) << 3;
      const unsigned int MSB = 1 << (SIZE-1);
      for (b=0; b<SIZE; b++)
      {
      cout << (( MSB & k) ? 1 : 0);
      k <<= 1;
      }
      cout << endl;
      }

      Or

      #include <iostream>
      using namespace std;
      void main()
      {
      unsigned int k = 731;
      unsigned int b;
      const int SIZE = sizeof(k) << 3;
      const unsigned int MSB = 1 << (SIZE-1);
      bool isLeadZero= true;
      for (b=0; b<SIZE; b++)
      {
      if ( MSB & k)
      {
      isLeadZero = false;
      cout << 1;
      }
      else
      {
      if ( ! isLeadZero) cout << 0;
      }

      	k <<= 1;
      }
      cout << endl;
      

      }

      If you need to remove heading zeroes. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      O 2 Replies Last reply
      0
      • CPalliniC CPallini

        You should go in the opposite way, using multiplication (or bit shif, that is faster):

        #include <iostream>
        using namespace std;
        void main()
        {
        unsigned int k = 731;
        unsigned int b;
        const int SIZE = sizeof(k) << 3;
        const unsigned int MSB = 1 << (SIZE-1);
        for (b=0; b<SIZE; b++)
        {
        cout << (( MSB & k) ? 1 : 0);
        k <<= 1;
        }
        cout << endl;
        }

        Or

        #include <iostream>
        using namespace std;
        void main()
        {
        unsigned int k = 731;
        unsigned int b;
        const int SIZE = sizeof(k) << 3;
        const unsigned int MSB = 1 << (SIZE-1);
        bool isLeadZero= true;
        for (b=0; b<SIZE; b++)
        {
        if ( MSB & k)
        {
        isLeadZero = false;
        cout << 1;
        }
        else
        {
        if ( ! isLeadZero) cout << 0;
        }

        	k <<= 1;
        }
        cout << endl;
        

        }

        If you need to remove heading zeroes. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        O Offline
        O Offline
        Omegaclass
        wrote on last edited by
        #3

        i just tried your code and it works very well, there is one thing, i just cant seem to understand how it works line by line, since i want to learn c++ i think it wise to at least understand how it works, could you explain how your code outputs so many leading zero's, i find that very interesting.

        CPalliniC 1 Reply Last reply
        0
        • O Omegaclass

          i just tried your code and it works very well, there is one thing, i just cant seem to understand how it works line by line, since i want to learn c++ i think it wise to at least understand how it works, could you explain how your code outputs so many leading zero's, i find that very interesting.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          In order to obtain the correct result (i.e. reversed bits) using your method, you need to multiply instead of dividing. At every iteration you have to:

          1. Test the most significative bit of the number.
          2. miltiply the number by two, so that the most significative bit is replaced by its adjacent one (the next you'll test).

          In my code I replaced multiplication by 2 with left shift that is faster. Dealing with MSB (most significative bit) is a little trcky since its position depends on the data type, i.e. it is bit 7 for unsigned chars, bit 15 for 2-byte data types (like short on my machine) and bit 31 for 4-byte data types (like unsigned int on my PC) hence the 'messy' lines in my routine, for instance:

          const int SIZE = sizeof(k) << 3;

          Here SIZE is set to the length, expressed in bits, of k (sizeof operator returns bytes, I multiply by 8 using, again, left shift operator). :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • CPalliniC CPallini

            You should go in the opposite way, using multiplication (or bit shif, that is faster):

            #include <iostream>
            using namespace std;
            void main()
            {
            unsigned int k = 731;
            unsigned int b;
            const int SIZE = sizeof(k) << 3;
            const unsigned int MSB = 1 << (SIZE-1);
            for (b=0; b<SIZE; b++)
            {
            cout << (( MSB & k) ? 1 : 0);
            k <<= 1;
            }
            cout << endl;
            }

            Or

            #include <iostream>
            using namespace std;
            void main()
            {
            unsigned int k = 731;
            unsigned int b;
            const int SIZE = sizeof(k) << 3;
            const unsigned int MSB = 1 << (SIZE-1);
            bool isLeadZero= true;
            for (b=0; b<SIZE; b++)
            {
            if ( MSB & k)
            {
            isLeadZero = false;
            cout << 1;
            }
            else
            {
            if ( ! isLeadZero) cout << 0;
            }

            	k <<= 1;
            }
            cout << endl;
            

            }

            If you need to remove heading zeroes. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            O Offline
            O Offline
            Omegaclass
            wrote on last edited by
            #5

            thank you very much, i will play with this code and see what i can do with this new information. you said you multiplied by 8, but i see only 3 could you explain this, if it was a typo then never mind

            CPalliniC 1 Reply Last reply
            0
            • O Omegaclass

              thank you very much, i will play with this code and see what i can do with this new information. you said you multiplied by 8, but i see only 3 could you explain this, if it was a typo then never mind

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              You are welcome. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              O 1 Reply Last reply
              0
              • CPalliniC CPallini

                You are welcome. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                O Offline
                O Offline
                Omegaclass
                wrote on last edited by
                #7

                i figured out what you meant by multiply by 8, it would be 2^3 which is 8 and what i seen in your code was 3. it seems this is a cool way to multiply and divide i hope this thread helps someone else learn something new as i did, thanks

                1 Reply Last reply
                0
                • O Omegaclass

                  hi everyone who reads this, i am in a digital electronics class and i decided to write a program that converts decimals to binary, i thought this was going to be simple and it was till i realized my my loop cout'ed the binary in reverse order. 731 in dec = 1011011011 and my program did it in reverse 1101101101 i used int's with modulus and division operators:

                  while (num)
                  {
                  rem=num%2;
                  num = num/2;
                  cout << rem;

                  }

                  so i was wondering is there a simple way to reverse the output or could the division be done in reverse so the last output is first and the first last in my program? please note i am a beginner at c++ so i don't know how to do arrays as of yet and i am sure the output could be loaded into an array and printed in reverse order. thank you in advanced for any help.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  There are many ways to do this. To use what you already have, add:

                  char s[128] = { '\0' };
                  int x = 0;

                  while (num)
                  {

                  = (num & 1) ? '1' : '0';

                  num = num / 2; // num >>= 1
                  x++;
                  }

                  cout << strrev(s) << endl;

                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  O 1 Reply Last reply
                  0
                  • D David Crow

                    There are many ways to do this. To use what you already have, add:

                    char s[128] = { '\0' };
                    int x = 0;

                    while (num)
                    {

                    = (num & 1) ? '1' : '0';

                    num = num / 2; // num >>= 1
                    x++;
                    }

                    cout << strrev(s) << endl;

                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    O Offline
                    O Offline
                    Omegaclass
                    wrote on last edited by
                    #9

                    its funny i was reading about arrays today and ways to reverse the output, had nothing yet, but i did learn how to make an array. what header files did you use? when i compile your modification to my code i get an unrecognizable output. thank you.

                    #include "stdafx.h"
                    #include using namespace std;

                    int main()
                    {

                    char s[128];
                    int x = 0;
                    int num =53

                    while (num)
                    {

                    = (num & 1) ? '1' : '0';

                    num = num / 2; // num >>= 1
                    }

                    cout << strrev(s) << endl;
                    system("pause");
                    return 0;
                    }

                    D 1 Reply Last reply
                    0
                    • O Omegaclass

                      its funny i was reading about arrays today and ways to reverse the output, had nothing yet, but i did learn how to make an array. what header files did you use? when i compile your modification to my code i get an unrecognizable output. thank you.

                      #include "stdafx.h"
                      #include using namespace std;

                      int main()
                      {

                      char s[128];
                      int x = 0;
                      int num =53

                      while (num)
                      {

                      = (num & 1) ? '1' : '0';

                      num = num / 2; // num >>= 1
                      }

                      cout << strrev(s) << endl;
                      system("pause");
                      return 0;
                      }

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      Have you used the debugger to step through the code watching s and num?

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      O 1 Reply Last reply
                      0
                      • D David Crow

                        Have you used the debugger to step through the code watching s and num?

                        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        O Offline
                        O Offline
                        Omegaclass
                        wrote on last edited by
                        #11

                        i haven't looked at the debugger messages, maybe i should, but as a beginner i may not even know what i am seeing so i will look into this. thanks

                        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