Convert to binary? Unsure about bitwise shift

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
static int 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??
Last edited on
First of all, you are using binary uninitialized. All numbers are stored in binary format so I don't really understand what baseNumber is doing here.
I think he wants to translate decimal number "7" to decimal number "111", and interpret the result as binary, regardless of the underlying meaning.

I can't really make heads or tails of your method though.
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?

btw, Gaminic, I'm a she. :)
Last edited on
Bump
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.
Maybe like this:

#include <bitset>

for(int i = 0; i < 16; i++)
{
	std::bitset<4> mybinary(i);
	std::cout << mybinary << std::endl;
	~mybinary;
}


Last edited on
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.
Thanks for all of the replies. I think I get it now.
Well this is what I changed the program to... however, I'm getting a stack error for the array, and I'm unsure why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	static int convertToDigits(int unconverted, int baseNumber)
	{
		int converted, firstDigit, secondDigit, thirdDigit, fourthDigit, temp, anothertemp;
		temp = unconverted;
		int trialArray[4];
		for(int i = 0; temp > 0; i++)
		{
			anothertemp = unconverted % baseNumber;
			temp = unconverted/ baseNumber;
			trialArray[i] = anothertemp;
		}
		firstDigit = trialArray[3];
		secondDigit = trialArray[2];
		thirdDigit = trialArray[1];
		fourthDigit = trialArray[0];
		

		converted = convertDigitsIntoNumber(firstDigit, secondDigit, thirdDigit, fourthDigit);
		return converted;
	}


This was from the suggestion of Resident biscuit... however, I am unsure as to how temp will ever be > 0... should it be

1
2
3
4
5
6
for(int i = 0; temp > 0; i++)
		{
			anothertemp = unconverted % baseNumber;
			temp = temp/ baseNumber;
			trialArray[i] = temp;
		}
Last edited on
The latter was the answer, although this is only good for binary.
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

now x = 43.

does anyone know how to do that?
Last edited on
By multiplying the digit with its place value and adding it to the total.
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
}
Last edited on
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.
That worked magically! Thank you very much, Athar.
Topic archived. No new replies allowed.