One way to multiply two integers A times B is to add A to itself B times. This i
ID: 3839188 • Letter: O
Question
One way to multiply two integers A times B is to add A to itself B times. This is not efficient for large numbers (100 times 100) as it requires a large number of additions. But there is a better way using bit shifts. 100 takes only 7 bits (01100100), and by using RLF or RRF operations along with addition the number of iterations can be significantly reduced-depending on the maximum sizes of the numbers allowed in a memory unit. The largest number one can store in the PIC16F88 is 225. With this in mind, consider mutliplying two 4-bit numbers together and putting the result into an 8-bit register by using shifting and adding. The table below shows a multiplication of 3 times 13 following such an algoirthm where the shifts rotate through carry ("car"). Each row shows the contents of A, B, and Cat the end of each iteration, along with the operations performed. The result,0010 0111, is 32 + 7 = 39, which is what you get with 3 times 13. As you can see by the above, when the shift of B sets the carry bit, then one adds A, otherwise one only completes t shifts. Regardless of the numbers A and B, the process always takes exactly 4 iterations. Translate the above algorithm into a functional code block that would allow the multiplication of any 4-bit integers. N.B. Why is it necessary to clear the carry bit between iterations? N. B. Where does B have to sit in a 8-bit register for the shifts to work properly .Explanation / Answer
def get_id(std):
return std[0]
def get_name(std):
return std[1]
def get_courses(std):
return std[2]
def get_fname(name):
return name[0]
def get_lname(name):
"""Returns last name"""
return name[1]
def get_ccode(course_det):
"""Returns course code part of the tuple"""
return course_det[0]
def get_grade(course_det):
"""Returns grade part of the tuple"""
return course_det[1]
st1=student("620000101","John","Doe","CS11Q",80,"CS11R",60,"CS20R",50,"CS20S",60,"CS22Q",65,"CS23Q",80)
"""Prints the students details and GPA"""
print ("Student Id:", get_id(std))
print ("Student name:", get_fname(get_name(std)), get_lname(get_name(std)))
print ("GPA: %.2f" %(calc_gpa(std)))
def compute_letter_grade(num):
if(num > 85):
return 'A+'
elif(num >=70 and num <= 85):
return 'A'
elif(num >= 67 and num <= 69):
return 'A-'
elif(num >= 63 and num <= 66):
return 'B+'
elif(num >= 60 and num <= 62):
return 'B'
elif(num >= 57 and num <= 59):
return 'B-'
elif(num >= 53 and num <= 56):
return 'C+'
elif(num >= 50 and num <= 52):
return 'C'
elif(num >= 47 and num <= 49):
return 'C-'
elif(num >= 43 and num <= 46):
return 'D+'
elif(num >= 36 and num <= 42):
return 'D'
elif(num <= 35):
return 'F'
def calc_letter_grade(student):
course = []
courselist=get_courses(student)
number_grade= [x[1] for x in courselist]
course_code= [x[0] for x in courselist]
letterGrade=map(compute_letter_grade,number_grade)
course=zip(course_code,letterGrade)
return course
def convert_to_wtqp(codegrade):
result1 = 0
result2 = 0
for my_key_cc,my_value_cc in credit_list.items():
if my_key_cc == get_ccode(codegrade).upper():
result1 = my_value_cc
for my_key_lgrade,my_value_lgrade in qp_list.items():
if my_key_lgrade == get_grade(codegrade):
result2 = my_value_lgrade
return (result1,result2)
lst_cc_lgrade = calc_letter_grade(std)
for i in lst_cc_lgrade:
cc_lgrade_value.append(convert_to_wtqp(i))
for i in cc_lgrade_value:
cc_grade_point.append(i[0]*i[1])
result=(sum(cc_grade_point)/sum(cc_credit_weight))
return result
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.