Thread: Conversion from INT to Binary

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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 04:51 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    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++.

  4. #4
    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

  5. #5
    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

  6. #6
    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.

  7. #7
    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 07:07 AM.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Well according to mathworld there have been mathematicians using bases that weren't even rational numbers. Someone for examples used numbers written in base pi (not quite sure how that one worked).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 11-30-2007, 03: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, 10:33 AM
  4. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM