Binary traslator

This is a discussion on Binary traslator within the C++ Programming forums, part of the General Programming Boards category; I took that challenge on this site to change decimal to binary, it works ok exept the binary numbers come ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Binary traslator

    I took that challenge on this site to change decimal to binary, it works ok exept the binary numbers come out in reverse. Any way to fix?(i stuck a \n in there to make it go up and down, makes it a little better)

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        int deci = 1;
            const int base2=2;
            cout<< "Enter a number you want in binary:  ";
       for(;;) 
        {if(deci == 0)
        {cout<< "Enter another number you want in binary:  ";}
        
        cin>> deci;
        cin.ignore();
        
        while (deci >= 0)
       {  
        cout<< " " <<deci % base2 << "\n";
        deci = deci / base2;
        if (deci == 0)
        break;
        }
       continue;
    }    
          cin.get();
        
        return 0; }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    There are several ways you could do this. One is to keep a vector/list of digits found and print them out in reverse, which might be too advanced for you. Another is to move your code into a recursive function that prints out the digits only at the end of the function (meaning all later digits will have already been printed out). This also is difficult if you don't know functions or recursion.

    Perhaps the way to do it with the least advanced features is to keep a decimal value of the current result. So each digit you create you add to the result. The trick is to multiply by a one more power of ten each time. Basically, start with multiplier = 1 (which is 10 to the 0 power). Then at the end of the loop multiply the multiplier by 10 so that it refers to the next higher digit. Instead of outputting the deci % base2 value, multiply it by the multiplier and add it to the result. When the loop is done the result will have the correct value in base2 and you can print it out.

    Example: decimal = 13.

    Result = 0
    Multiplier = 1

    Loop 1
    Base2 digit = decimal % 2 = 13 % 2 = 1
    decimal = decimal / 2 = 13 / 2 = 6
    Result = Result + (Base2 digit * Multiplier) = 0 + (1 * 1) = 1
    Multiplier = Multiplier * 10 = 1 * 10 = 10

    Loop 2
    Base2 digit = decimal % 2 = 6 % 2 = 0
    decimal = decimal / 2 = 6 / 2 = 3
    Result = Result + (Base2 digit * Multiplier) = 1 + (0 * 10) = 1
    Multiplier = Multiplier * 10 = 10 * 10 = 100

    Loop 3
    Base2 digit = decimal % 2 = 3 % 2 = 1
    decimal = decimal / 2 = 3 / 2 = 1
    Result = Result + (Base2 digit * Multiplier) = 1 + (1 * 100) = 101
    Multiplier = Multiplier * 10 = 100 * 10 = 1000

    Loop 4
    Base2 digit = decimal % 2 = 1 % 2 = 1
    decimal = decimal / 2 = 1 / 2 = 0
    Result = Result + (Base2 digit * Multiplier) = 101 + (1 * 1000) = 1101
    Multiplier = Multiplier * 10 = 1000 * 10 = 10000

    decimal <= 0 --> break.

    Result is 1101. Output.
    Last edited by Daved; 08-23-2005 at 07:07 PM.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, you'll want to start at the high order bits instead of the low order bits. So, what you'll really need to do is use a mask to compare binary values, one bit at a time (I am assuming that you don't want to use std::bitset).

    To shift your mask, you will want to use the bitshift operators >>, starting with only a 1 in the high order position, and then shift it over, and compare using &.

    So, an example of one test.
    unsigned char num = 14; // unsigned char used because it is small
    unsigned char mask = 128;

    print 1 iff (num & mask != 0)
    print 0 otherwise

    So, in the example,
    num is 0000 1110 and mask is 1000 0000, so you print 0
    Then, you shift mask over one space and repeat

    And, the best way to get a 1 in the highest order position for an unsigned integral type would be:
    Code:
    #include <limits>
    unsigned int mask = 1 << (std::numeric_limits<unsigned int>::digits - 1);
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 01:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21