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. #1
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    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. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Can you use bitwise operators?

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i am soooooo doomed in this part.....


    do i REALLY need this in my career?

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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){
    		cout << ((mask & x)?'1':'0');
    		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....
    Last edited by Fordy; 09-17-2002 at 01:25 AM.

  7. #7
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    >> 1110101 = 117....not 85..

    That's because 2 \ 2 = 1 1

    should be 2 / 2 = 1 0
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  8. #8
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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. #9
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    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.

    start with 1 then start multiplying by 2.

    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.
    Best Regards,

    Bonkey

  11. #11
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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. #12
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    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?
    Best Regards,

    Bonkey

  13. #13
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i am so dumb i'm never gonna get this....

  14. #14
    Registered User
    Join Date
    Aug 2002
    Posts
    170

    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
    Last edited by bonkey; 09-16-2002 at 03:51 PM.
    Best Regards,

    Bonkey

  15. #15
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary conversion..
    By shwetha_siddu in forum C Programming
    Replies: 3
    Last Post: 07-31-2008, 04:24 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. char to binary conversion
    By Niara in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2005, 06:47 AM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Replies: 5
    Last Post: 11-21-2004, 04:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21