I'm unsure of how to use a bitwise shift or if I should even use one, but I am trying to convert a number in base 10 into binary. I thought this was the simplest way to go, but I'm getting the wrong answer, and obviously my program doesn't make much sense, which is why I need some help. This is in it's own class, which is why it's a static int.
**EDITED 4/6/12 12:42PM MST**
1 2 3 4 5 6 7 8 9 10 11 12 13
staticint binaryUnsignedInt(int unconverted, int baseNumber)
{
int binary,temp, math;
for(int i = 0; i < unconverted; i++)
{
math = unconverted - i;for(j = 0; j < 2; j++)
{
temp = j;
}
binary = binary & (temp>>1);
}
return binary;
Unsure about the changes... I was thinking I could potentially divide the base ten number by 1000, 100 and 10, and then take each of those anwers and mod 2 them??
Thanks for pointing out I didn't initialize binary. baseNumber is there so that it can be easily changed to another base, if another programmer were to use it. Yes, I want the number 15 to look like "1111". My method may be wrong, I've never done this before, nor do I speak, think or understand binary, although I do know how to count it... 0, 1, 10, 11, 100, 101, 110, 111, 1000... etc. I jsut don't know how to make the program. My last post is my effort, I just need some help. How does a bitwise shift work? and how can I fix the program above?
Bit shifting won't help you with this, as it shifts digits only in the binary representation of a number, while you need to shift digits in the decimal representation of the number.
Extract the last digit from the unconverted value (using modulo like you do now), then get rid of that digit by shifting one to the right (using division) and add the digit multiplicated by its place value (1, 10, 100, 1000 etc.) to the converted value.
For example, the number 13 becomes 1101 = 1*1000 + 1*100 + 0*10 + 1*1.
Converting base 10 to any base follows the same method each time. You could creat a functions that takes in two parameters, base 10 number and base to convert to. Steps involved in converting are as follows:
1. Mod base 10 number by base to convert to. Record this result
2. Divide base 10 number by base to convert to. Record this in separate variable
3. Repeat steps 1 and 2 until step 2 yields and answer < 0.
4. The answers given from step 1 will be your number, in reverse order.
I'm on my phone, so giving an example would be kind of a pain. If you need an example, I'll be at my computer later.
That's still not right, it must be temp % baseNumber. And it works for all bases.
Why are you using an array? What if you pass values with more than 4 binary digits (>=16)?
Also, it only goes up to 4 digits, the way I have it, which is only up to 15 in base 10. The reason why I was looking about the bitwise shift is because I wanted to tell the program:
"Take this number and move it to the right one place"
example:
//some code, probably a loop, which is why x changes
x= 3
//move x over one place to the right
x=4
for binary, that's a long list to convert. example: 100 in base 10 is 1100100 in binary, for which the code gets pretty long, and that's only to 100 in decimal. Example 100 in decimal to binary:
1 * 1000000 +
1 * 100000 +
0 * 10000 +
0 * 1000 +
1 * 100 +
0 * 10 +
0
instead, I would like the program to shift the number over, which I can find the answer, jsut need to know how to shift
1 2 3 4 5 6
for(i =0; i > highestValue; i++)
{
x = binaryNumber;
binary = binary & //x shifts to right
}
There's no list at all, that's what loops are for.
If you want to shift one to the right in the decimal representation, divide by 10.
If you want to shift one to the left, multiply by 10.