Conversion from INT to Binary

This is a discussion on Conversion from INT to Binary within the C++ Programming forums, part of the General Programming Boards category; A quick question: How do you convert an integer to a binary number?...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Conversion from INT to Binary

    A quick question: How do you convert an integer to a binary number?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Signed or unsigned?

    Unsigned:
    1. Take the remainder of # / 2.
    2. Divide the number by two
    3. if new number is 0 stop
    4. else go back to step 1

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    so there arent any commands to do this, you have to make like a function to convert it? Thank you by the way

  4. #4
    Bob Dole for '08 B0bDole's Avatar
    Join Date
    Sep 2004
    Posts
    618
    yah, I came up with one real quick and posted it, then thought it would be fun for you to do it yourself, but basicly yes.
    Hmm

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    Great site. I don't know why I would need to know how to do that yet, but when it comes up I know how now so thanks.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by Junior89
    so there arent any commands to do this, you have to make like a function to convert it? Thank you by the way
    If you are just wanting to display the binary equivalent of a base 10 integer, you can go the easy way and use a bitset to do this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> A quick question: How do you convert an integer to a binary number?

    just to clarify: an integer is a value and a base is a representation of a value. by representation I mean a string of characters where each character is referred to as a digit and each digit corresponds a value in the range of 0 through base-1. so technically, you don't convert an integer to a specific base - you convert one base representation of an integer to another base representation. here's a similar thread that you might find helpful.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    43
    Quote Originally Posted by Sebastiani
    >> A quick question: How do you convert an integer to a binary number?

    just to clarify: an integer is a value and a base is a representation of a value. by representation I mean a string of characters where each character is referred to as a digit and each digit corresponds a value in the range of 0 through base-1. so technically, you don't convert an integer to a specific base - you convert one base representation of an integer to another base representation. here's a similar thread that you might find helpful.
    While we're talking of base numbers, maybe you could help with this question:

    How do you represent a number in base minus one?


    Now my thoughts are that using the sequence below it is impossible to even have a base positive 1 yet alone minus 1.

    Base 10 has numbers 0,1,2,3,4,5,6,7,8,9
    Base 8 has 0,1,2,3,4,5,6,7
    Base 2 has 0,1
    Base 1 has 0

    Since based 1 has only 1 value then it cannot represent information. So how does base minus 1 work?

    The question in bold above, was from the book Hard Drive which is a history of the Microsoft company. Apparently this question used to be used to test employees in the early days.

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I always think of base one like this:
    Position n is worth 1^n = 1, so every position is equally worth.
    1=1
    2=11
    3=111
    4=1111
    ...
    This isn't 100% consistent with the other bases as you have to drop the zero.
    Also, non-integers are hard to represent like this.

    EDIT:
    As for the conversion, I'm quite sure I've posted such code here before. Do a board search and I'm sure you'll encounter code by me or someone else.

    Here's how it could look like:
    Code:
    string num2str(unsigned num, unsigned base)
    {
      const char digits['0','1',...'9','A',...'Ö']; //the digits we will use to build the number
      string output;
      while (num > 0)
      {
        output = output + digits[num%base];
       num /= base;
      }
      return output;
    }
    Add some error checking and it should work.
    Last edited by Sang-drax; 12-30-2004 at 03:51 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Only reason that wouldn't work in electronics, is because it wouldn't know the "start" or the "end" of things.

    You would have to have base 2 if you were to have a start and an end. But representing it on paper, and not a duration with a stop/start value, works just fine. It just doesn't work well in the world of electronics.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by chris1985
    How do you represent a number in base minus one?
    After a quick look on google and mathworld I'd say it is impossible.
    The representation of a number in base b is equivalent to a polynomial
    x1 b^0 + x2 b^1 + x3 b^2 + ... + x4 b^n

    where n is floor(log(number, base))

    now since for any number (except 1 and -1) the logarithm of that number in base -1 is a complex number I fail to see how you could write the polynomial (most likely due to my limited knowledge of mathematics)

    More over the digits are computed as:
    xn = floor(numberi/basei)

    where numberi (i is a subscript) is defined as

    numbern = number

    number(i-1) = numberi - (xi base^i)

    again this formula fails for base = -1 (infinite recursion in the case of a complex exponent for base)

    Now, I'm not a mathematician and if anyone who actually has studies this stuff wants to shed further light on the subject I'd be grateful

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by sigfriedmcwild
    After a quick look on google and mathworld I'd say it is impossible.
    The representation of a number in base b is equivalent to a polynomial
    x1 b^0 + x2 b^1 + x3 b^2 + ... + x4 b^n

    where n is floor(log(number, base))

    now since for any number (except 1 and -1) the logarithm of that number in base -1 is a complex number I fail to see how you could write the polynomial (most likely due to my limited knowledge of mathematics)

    More over the digits are computed as:
    xn = floor(numberi/basei)

    where numberi (i is a subscript) is defined as

    numbern = number

    number(i-1) = numberi - (xi base^i)

    again this formula fails for base = -1 (infinite recursion in the case of a complex exponent for base)

    Now, I'm not a mathematician and if anyone who actually has studies this stuff wants to shed further light on the subject I'd be grateful

    This is true:
    The representation of a number in base b is equivalent to a polynomial
    x1 b^0 + x2 b^1 + x3 b^2 + ... + x4 b^n
    The base, b, does not have to be positive.

    If you are representing positive integers in a system with positive base b the expession floor(log(number, base)) gives a way of determining the number of base-b digits that would be required. (But it has nothing to do with the representation itself, or with the definition.)

    Using base -2, here are a few numbers

    Code:
     Base 10     Base -2
    
     
         -10        1010
          -9        1011
          -8        1000
          -7        1001
          -6        1110
          -5        1111
          -4        1100
          -3        1101
          -2        10
          -1        11
           0        0
           1        1
           2        110
           3        111
           4        100
           5        101
           6        11010
           7        11011
           8        11000
           9        11001
          10        11110
    Regards,

    Dave

  14. #14
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    @Dave Evans
    With a base of -1 though you are still limited to a string of +1 and -1 that are multiplied by some coefficient (my guess is that you'd have a single coefficient: 0) making the results an infinite string (or just plain impossible).

    Or maybe I'm looking at this completely wrong.

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by sigfriedmcwild
    @Dave Evans
    With a base of -1 though you are still limited to a string of +1 and -1 that are multiplied by some coefficient (my guess is that you'd have a single coefficient: 0) making the results an infinite string (or just plain impossible).

    Or maybe I'm looking at this completely wrong.
    You are, of course, correct. My example shows that it is possible to have negative number base systems. It just doesn't lead anywhere for base -1. (In other words, I committed the classic mistake of answering a question that wasn't asked --- I just didn't think it through.)


    Regards,

    Dave
    Last edited by Dave Evans; 01-01-2005 at 06:07 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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