# Lesson: Binary conversion n back again

This is a discussion on Lesson: Binary conversion n back again within the Windows Programming forums, part of the Platform Specific Boards category; Ok today in class we learned how to take a number and make it binary, no big, but to take ...

1. ## Lesson: Binary conversion n back again

Ok today in class we learned how to take a number and make it binary, no big, but to take a random binary and make it back to a number....whoa...

This i can do:

85 to binary:

\ = divide

85 \ 2 = 42.5 1

42 \ 2 = 21 0

21 \ 2 = 10.5 1

10 \ 2 = 5 0

5 \ 2 = 2.5 1

2 \ 2 = 1 1

1 \ 2 = 1 1

So 85 in binary is 1110101

but to take say 101011010(random) and make it a regular number, i'm lost, any help?

2. Can you use bitwise operators?

3. i have no idea what that is :P

Its not a program its paper -bound, the one example was something like this: the (#) are the exponent value

100111

1 x 8(10) + 0 x 8(9) + 0 x 8(8) + 1 x 8(7) + 1 x 8(6) + 1 x 8(5)

4. It's still easy if you do it on paper:

example: 101011010
integer value = 0*2^0 + 1*2^1 + 0*2^2 + 1*2^3... + 1*2^8

It's the same concept as the one in base 10:
3129
= 9*10^0 + 2*10^1 + 1*10^2 + 3*10^3
= 9 * 1 + 2 * 10 + 1 * 100 + 3 * 1000

5. i am soooooo doomed in this part.....

do i REALLY need this in my career?

6. I dispair if your teacher did teach you to do it that way....

1110101 = 117....not 85..

Code:
```#include <iostream>
#include <cstring>
using std::cout;
using std::endl;

void ToBin(int x){
const int mask = 1 << ((8 * sizeof(int))-1);

while(x){
x <<= 1;
}
cout << endl;
}

void ToDec(char* y){

int mult = 1,
total = 0;
int len = strlen(y);
char* c = y + len - 1;

for(int i = 0;i<len;i++,c--){
if((char)*c == '1')
total += 1*mult;
mult *= 2;
}

cout << total << endl;
}

int main( void )
{
ToBin(85);

ToDec("1010101");

return 0;
}```
Here's one quick way of converting to and fro....

7. >> 1110101 = 117....not 85..

That's because 2 \ 2 = 1 1

should be 2 / 2 = 1 0

8. Ok lemme give u one of her off-the-board ones cause i may have done that wrong.

According to her

78 in binary is:

1001110

I had a chart showing the binary of each number up to 10 but i lost it.

9. According to her

78 in binary is:

1001110
Yes that's right.

>>do i REALLY need this in my career?
Yes

>>I had a chart showing the binary of each number up to 10 but i lost it.
F*** the chart.

10. Originally posted by Ride -or- Die
Ok lemme give u one of her off-the-board ones cause i may have done that wrong.

According to her

78 in binary is:

1001110

I had a chart showing the binary of each number up to 10 but i lost it.
Yeah I get to teach someone something.

CONVERSIONS ARE EASY.

can you muliply by 2? Then you can convert decimal to binary.

1 2 4 8 16 32 64 128 256 512 1024 2048 ...... you can go on forever.

Now reverse these numbers.
64 32 16 8 4 2 1 and line up your binary number to the right.
1 0 0 1 1 1 0
64 + 8 + 4 + 2 = 78 wasnt that easy?

going the other way is easy too, not as easy. Start with the bigest number that is smaller than the number you want to find. So 64. Thats a 1. Now subtract 64 from 78 = 14 now find the largest number that is less that 14. 8. 14-8 = 6. so 4 then 2. You get the idea.

I find this to be the most easy to understand method.

11. 1 2 4 8 16 32 64 128 256 512 1024 2048 ...... you can go on forever.

Now reverse these numbers.
64 32 16 8 4 2 1 and line up your binary number to the right.
1 0 0 1 1 1 0
64 + 8 + 4 + 2 = 78 wasnt that easy?
Ok this part throws me. I get the x2 on and on, its the connection between say 64 and one, why is 61 a 1?? thats where i get lost.

12. When i say 64 is a one, we are just building our binary number. We mark (on our number chart) a 1 for the number we use and 0 for the numbers we dont use.

So lets take another numer say 115.

On our scale 128 > 115 so its too big. (its a 0)
115 > 64 so 64 will be marked as a 1. (we use it to make our binary number. )

Then we subtract 64 from 115 = 51. So using the same logic. we mark a 1 under the 32. Because 32 is the biggest number that is smaller than 51. Then we subtract the 32 that we used. And we have 19 left to deal with.

So our next number will be 16 (smaller than 19). We mark it with a 1. and subtract 16. to get 3.

What is the biggest number on our chart smaller than 3? 2 so we mark a 1 under 2. Now 8 and 4 were skipped, so mark them with a 0.

we subtract 2 and that leaves 1 so mark the 1 with a 1.

64 32 16 8 4 2 1

1 1 1 0 0 1 1

so we see that 64 + 32 + 16 + 2 + 1 = 115.

Does that make sense?

13. i am so dumb i'm never gonna get this....

14. ## Re: Lesson: Binary conversion n back again

Originally posted by Ride -or- Die

but to take say 101011010(random) and make it a regular number, i'm lost, any help? [/B]
Let's do this one. First draw our number chart.

1024 512 256 128 64 32 16 8 4 2 1

Then starting from the right draw your binary numbers underneath it putting a 1 or a 0 under each number starting at the 1.

1024--512--256--128--64--32--16--8--4--2--1
-------------1----0----1---0---1---1--0--1--0

then just add up all the numbers that have a 1 under them.

256 + 64 +16 + 8 + 2 = 346

15. ok so lemme try one.

1001111011

1 2 4 8 16 32 64 128 256 512

1 1 0 1 1 1 1 0 0 1

then its

1
2
8
16
32
64
+512
_______

635

so 1001111011 = 635?

I am rlly trying i swear lol.

Page 1 of 2 12 Last