Add numbers in hexadecimal base without converting bases?

11,076

You can have a sub-function that adds two single-digit hex numbers and returns their single-digit sum and a carry (either 0 or 1). This function will take three inputs: two numbers you want to add and a carry-in. You can then loop through the digits of the two numbers you want to add from least significant to most significant, and apply this function for every pair of digits while taking into account the carry at each stage.

So let's try your example:

A 5
1 7 +

We start at the least significant digits, 5 and 7, and perform the 1-digit addition. 516 + 716 = 1210. 1210 is less than 1610, so the output of our 1-digit add is 1210 = C16 with a carry of 0.

Now we add A and 1 (our carry-in is 0 so we can just add them normally). A16 + 116 = 1110. 1110 is less than 1610, so the output of our 1-digit add is 1110 = B16 with a carry of 0. (If we had a non-zero carry-in, we would just add 1 to this value.)

Hence, our overall result is:

A 5
1 7 +
-----
B C
Share:
11,076
Tam211
Author by

Tam211

Software engineering student

Updated on June 04, 2022

Comments

  • Tam211
    Tam211 almost 2 years

    I need to write a function which gets two numbers in hexadecimal base, and calculates the sum of both of them, I'm not allowed to convert them to decimal base, the code is supposed to calculate it "manually" using loops. for example this is how it should work:

     1
     1 f 5  (A) 
    +  5 a  (B) 
    ------------- 
    = 2 4 f 
    

    Here is an input example: >>> add("a5", "17") 'bc'

    I've started building my code but I got stuck, I thought I would divide into three ifs, one that would sum up only numbers, other sums numbers and letters, and the third one sums letters, but I don't know how to continue from here:

    def add_hex(A,B):
    lstA = [int(l) for l in str(A)]
    lstB = [int(l) for l in str(B)]
    
    if len(A)>len(B):
        A=B
        B=A
    A='0'*(len(B)-len(A))+A
    remainder=False
    result=''
    for i in range(len(B)-1)
    if (A[i]>0 and A[i]<10) and (B[i]>0 and B[i]<10):
       A[i]+B[i]=result
       if A[i]+B[i]>10:
           result+='1'
    

    Any help is greatly appreciated, I have no clue how to start on this!