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

Printable View

- 12-29-2004Junior89Conversion from INT to Binary
A quick question: How do you convert an integer to a binary number?

- 12-29-2004Thantos
Signed or unsigned?

Unsigned:

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

- 12-29-2004Junior89
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-2004B0bDole
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-2004sand_man
i found a good link for you

http://www.goshen.edu/compsci/mis200/decbinary.htm - 12-29-2004Emotions
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-2004hk_mp5kpdwQuote:

Originally Posted by**Junior89**

*bitset*to do this. - 12-30-2004Sebastiani
>> 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-2004chris1985Quote:

Originally Posted by**Sebastiani**

**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-2004Sang-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;

}

- 12-30-2004Tronic
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-2004sigfriedmcwildQuote:

Originally Posted by**chris1985**

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-2004Dave EvansQuote:

Originally Posted by**sigfriedmcwild**

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

*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

Dave - 01-01-2005sigfriedmcwild
@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-2005Dave EvansQuote:

Originally Posted by**sigfriedmcwild**

Regards,

Dave