Click here to Skip to main content
15,884,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What should be the approach to print a 64 bit Integer value in Decimals with a 32-bit processor ?

Recently asked in an Interview.
Posted

In the sake of simplicity, suppose you have a 8-bit-only processor and want to show a 16 bit unsigned number.

Assuming n is the input number, the algorithm could be (pseudo code):
  1. k = 0
  2. if n = 0 exit
  3. r[k] = reminder of n div 10
  4. n = n div 10
  5. k = k + 1
  6. goto 2

(where div is the integer division)

At the end, the array r contains the digits of the decimal representation of the number (in reverse order).

Of course there is a problem, in the given algorithm: we have to compute the integer division (and the reminder) of a 16 bit number with a 8-bit-only processor!

We could perform this operation like we do by hand, but using 4 bits at time, in other word using the hexadecimal representation of the numbers.

Let's try to do it in an example.
Namely we try to divide n = 51728 by 10, that is, in hexadecimalese, 0xCA10 by 0xA.
0xCA is the most significative byte and 0x10 is the least significative byte.
In the first step we divide the upper nibble of 0xCA, that is 0xC by 0xA, obtaining 0x1 as quotient and 0x2 as reminder.
Then we add such reminder to the lowest nibble of 0xCA (namely 0xA) in order obtain 0x2A and repeat the process:
// almost all hexadecimal numbers, here
 CA10            |A
                 ---
 2A               1           (C/A is 12/10, that is quotient 1, reminder 2)
  21               4          (2A/A is 42/10, that is quotient 4, reminder 2)
   30               3         (21/A is 33/10, that is quotient 3, reminder 3)
    6                4        (30/A is 48/10, that is quotient 4, reminder 8)

hence we have obtained the quotient, namely 0x1434 (5172 in decimal) and the reminder 8, always working with, at most, a byte at time. In other words, our 8-bit-only processor can perform it.
 
Share this answer
 
v2
Comments
Vishal_kj 22-Jan-14 8:57am    
Don't you think it is a very complex algorithm which could be very hard to implement because we have to perform shift operation multiple times. Another problem with this approach is, for a 64-bit integer, taking 4-bits at a time might be much complex than it is for a 16-bit integer.

As i'm just a beginner, it is too difficult for me to explain such complex algorithm to the interviewer. I had explained a similar approach to the Interviewers but they were not satisfied with my answer.
CPallini 22-Jan-14 9:13am    
It looks a simple algorithm, to me.
64 bits integers are, of course, wider than 16 bit integers, however taking 4 bits at time is not 'more complex'.
If you don't master the algorithm then, yes, you are not able to properly explain it to the interviewer.
For sure there are algorithms better than this one. Consider it 'my two cents contribution' (anyway, I guess it would actually work).
Vishal_kj 22-Jan-14 9:30am    
I am totally agree with you Sir!

I shall try to implement and master this approach.
Thanks a ton!
[no name] 22-Jan-14 23:32pm    
Excellent.
CPallini 23-Jan-14 2:49am    
Thank you very much.
Prabably tt is not so good, however it took some effort.
You can try

printf("%I64d",c64);
 
Share this answer
 
Comments
Vishal_kj 22-Jan-14 6:00am    
There must be a compiler for that 32-bit processor for using this statement.

I am asking the approach or any logic for printing or converting that 64-bit integer value into ASCII characters so that it could print the decimal value as a string.
( Assume you don't have any compiler for that 32-bit processor)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900