5
votes

This is surely a duplicate, but I was not able to find an answer to the following question.

Let's consider the decimal integer 14. We can obtain its binary representation, 1110, using e.g. the divide-by-2 method (% represents the modulus operand):

14 % 2 = 0
 7 % 2 = 1
 3 % 2 = 1
 1 % 2 = 1

but how computers convert decimal to binary integers?

The above method would require the computer to perform arithmetic and, as far as I understand, because arithmetic is performed on binary numbers, it seems we would be back dealing with the same issue.

I suppose that any other algorithmic method would suffer the same problem. How do computers convert decimal to binary integers?

Update: Following a discussion with Code-Apprentice (see comments under his answer), here is a reformulation of the question in two cases of interest:

a) How the conversion to binary is performed when the user types integers on a keyboard?

b) Given a mathematical operation in a programming language, say 12 / 3, how does the conversion from decimal to binary is done when running the program, so that the computer can do the arithmetic?

3

3 Answers

0
votes

There is only binary

The computer stores all data as binary. It does not convert from decimal to binary since binary is its native language. When the computer displays a number it will convert from the binary representation to any base, which by default is decimal.

A key concept to understand here is the difference between the computers internal storage and the representation as characters on your monitor. If you want to display a number as binary, you can write an algorithm in code to do the exact steps that you performed by hand. You then print out the characters 1 and 0 as calculated by the algorithm.

0
votes

Indeed, like you mention in one of you comments, if compiler has a small look-up table to associate decimal integers to binary integers then it can be done with simple binary multiplications and additions. Look-up table has to contain binary associations for single decimal digits and decimal ten, hundred, thousand, etc. Decimal 14 can be transformed to binary by multipltying binary 1 by binary 10 and added binary 4. Decimal 149 would be binary 1 multiplied by binary 100, added to binary 4 multiplied by binary 10 and added binary 9 at the end.

0
votes

Decimal are misunderstood in a program

let's take an example from c language

int x = 14;

here 14 is not decimal its two characters 1 and 4 which are written together to be 14

we know that characters are just representation for some binary value
1 for 00110001
4 for 00110100

full ascii table for characters can be seen here

so 14 in charcter form actually written as binary 00110001 00110100
00110001 00110100 => this binary is made to look as 14 on computer screen (so we think it as decimal)

we know number 14 evntually should become 14 = 1110
or we can pad it with zero to be
14 = 00001110

for this to happen computer/processor only need to do binary to binary conversion i.e.
00110001 00110100 to 00001110
and we are all set