# Conversion from INT to Binary

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-29-2004
Junior89
Conversion from INT to Binary
A quick question: How do you convert an integer to a binary number?
• 12-29-2004
Thantos
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
• 12-29-2004
Junior89
so there arent any commands to do this, you have to make like a function to convert it? Thank you by the way
• 12-29-2004
B0bDole
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.
• 12-29-2004
sand_man
• 12-30-2004
Emotions
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.
• 12-30-2004
hk_mp5kpdw
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.
• 12-30-2004
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.
• 12-30-2004
chris1985
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.
• 12-30-2004
Sang-drax
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.
• 12-30-2004
Tronic
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.
• 12-31-2004
sigfriedmcwild
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
• 12-31-2004
Dave Evans
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:
Quote:

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
• 01-01-2005
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.
• 01-01-2005
Dave Evans
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
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last