A quick question: How do you convert an integer to a binary number?
A quick question: How do you convert an integer to a binary number?
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
so there arent any commands to do this, you have to make like a function to convert it? Thank you by the way
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
i found a good link for you
http://www.goshen.edu/compsci/mis200/decbinary.htm
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.
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.Originally Posted by Junior89
"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
>> 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:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
While we're talking of base numbers, maybe you could help with this question: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.
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:
Add some error checking and it should work.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; }
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
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++.
After a quick look on google and mathworld I'd say it is impossible.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
Originally Posted by sigfriedmcwild
This is true:
The base, b, does not have to be positive.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
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
Regards,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
@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.)Originally Posted by sigfriedmcwild
Regards,
Dave
Last edited by Dave Evans; 01-01-2005 at 07:07 AM.