• 0

decimal to binary


Question

can anyone show me how to convert from decimal numbers (ie. -12, 8, 14 -3 etc...) to binary numbers (ie. 00001010) in ones complement and two's complement...? and how to convert between ones complement and two's complement with positive and negative numbers...? thanks in advanced.....^^

or is there sites that show how to do it...? thanks........

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

i use a table wen i convert decimal to binary

binary's base is always 2

and u go backwards

lets start with 6 powers

2^6 2^5 2^4 2^3 2^2 2^1 2^0

64 32 16 8 4 2 1

lets take for example a decimal number 20

u see through the table which number goes into 20, well obviously 16 does

so there fore u have one thing that went into it

so u add a 1

then ur left with a remainder of 4 now u see which one it can go into it

the next one is perfect so u add a 1

now u have no remainders but u gotta fill up the spaces so u just add two 0's

there fore the binary number for 20 is 1100

im not a good explainer sorry..

Link to comment
Share on other sites

  • 0

:no: :wacko: :blink:

sorry but i don't get your explanation.... :blush:

can somebody explain it to me or suggest a site to me...?

other than converting to binary, i need to know more about ones and two's complement....

and also adding and subtracting binary numbers.........thanks in advanced...

Link to comment
Share on other sites

  • 0

Decimal to binary

First, know that just like each digit in a decimal number represents a power of ten (so the 2 in 1234 represents 2 x 10^2), each digit in a binary number represents a power of two. 1, 2, 4, 8, 16, 32, 64, 128, 256, etc. 101 (base 2) = (1 x 2^2) + (0 x 2^1) + (1 x 2^0).

So. Break the decimal number into it's power of two components. Then, for each bit, check if the corresponding power of two is present. If it is, the bit is a one, otherwise it's a zero.

For example:

42 = 32 + 8 + 2

42 = 2^5 + 2^3 + 2^1

42 = (1 x 2^5) + (0 x 2^4) + (1 x 2^3) + (0 x 2^2) + (1 x 2^1) + (0 x 2^0)

42 = 101010 (base 2)

29 = 16 + 8 + 4 + 1

29 = 2^4 + 2^3 + 2^2 + 2^0

29 = (1 x 2^4) + (1 x 2^3) + (1 x 2^2) + (0 x 2^1) + (1 x 2^0)

29 = 11101 (base 2)

If your wondering how to do the first step, it's easy. Just keep subtracting the highest power of two you can until you reach zero. Like for 42 (the first example):

42 - 32 = 10

10 - 8 = 2

2 - 2 = 0

So the powers of two are 32, 8 and 2.

PS: I'm tired, I'll answer the rest of your question later :p

PS2: By the way Winston, binary for 20 is not 1100, it's actually 10100. :blink:

Link to comment
Share on other sites

  • 0

The Number -4:

(0100) = 4

(1100) = -4

Complement all bits and add 1, or complement every bit (going from right to left) after the first 1.

In 2's complement the most significant bit represents a negative number. in this case -8, all the other bits are positive numbers.

in 1's complement you simply complement all bits.

Link to comment
Share on other sites

  • 0

Now after looking at the relpies answering how to convert from decimal to binary, I realized how impractical that is for programming. What happens if you want to convert a number like 781,456,945? Are you going to use the powers of 2? No. There is a really cool way of doing it that involves a simple for loop and modulus (% operator). The modulus of 2 numbers is the remainder after division. So 5%2=1 because 5/2 = 2 remainder 1. If you already knew what modulus is then great! Anyway, if you want to convert 42 to binary do the following:

Starting with 42%2:

42/2 = 21 R 0

21/2 = 10 R 1

10/2 = 5 R 0

5/2 = 2 R 1

2/2 = 1 R 0

1/2 = 0 R 1

Reading the remainders (moduluses) from the bottom you get 101010. There you go!

If you want to know how to convert to decimal from binary, I know another really simple way involving synthetic division. Just contact me in anyway you want to learn how.

Edited by jl7c2
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.