Question

Write a function: that, given two non-negative integers A and B, returns the number of bits...

Write a function: that, given two non-negative integers A and B, returns the number of bits set to 1 in the binary representation of the number A * B. For example, given A = 3 and B = 7 the function should return 3, because the binary representation of A * B = 3 * 7 = 21 is 10101 and it contains three bits set to 1. Assume that: In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment

3 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #2

def solution(A,B):

    C =A*B

    bin_c = bin(C).replace("0b","") #converting to binary form

    res = [int(x) for x in str(bin_c)] #converting binary number to list of 0 and 1

    c_1 = 0 #counter for 1

    for i in range(len(res)): #counting the number of 1's in list

    if res[i]== 1:

        c_1 = c_1 + 1 

    return c_1


answered by: codegates
Add a comment
Answer #1

int countbits(unsigned int a, unsigned int b) {

unsigned int n = a * b;

unsigned int count = 0;

    while (n) {

        count += n & 1;

        n >>= 1;

    }

    return count;

}

Add a comment
Answer #3

def checkSorted(a, b):

    output = a*b

    if output == 0:

        return 0

    else:

    binResult = bin(output)[2:]

    return binResult.count("1")

 

result = checkSorted(3,7)

print(result)


answered by: anonymous
Add a comment
Know the answer?
Add Answer to:
Write a function: that, given two non-negative integers A and B, returns the number of bits...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q3 [Number Searching] (25%) Given two non-negative integers A (data type: int) and B (data type:...

    Q3 [Number Searching] (25%) Given two non-negative integers A (data type: int) and B (data type: int), write a C++ program to determine whether A contains B, wherein "contains” means an exact match. For example, • If A is 12345 and B is 234, A contains B, where the matching part is underlined in A. • If A is 12345 and B is 235, A does not contain B, even A has the digits 2, 3 and 5. It is...

  • Parvati is given an array of integers. She is asked to return the sum of all...

    Parvati is given an array of integers. She is asked to return the sum of all the two-digit numbers in the array. Please help her to perform this task. Write a function: class Solution { public int solution (int[] A); } that, given an array A consisting of N integers, returns the sum of all two-digit numbers. For example, given A = [1, 1000, 80, -91], the function should return -11 (as the two-digit numbers are 80 and -91). Given...

  • Give an informal description (in plain English) of a Turing machine with three tapes that receives as input two non-negative integers x and y

    Give an informal description (in plain English) of a Turing machine with three tapes that receives as input two non-negative integers x and y, and returns as output the integer xy. Integers are represented as binary strings.Start of the computation: The first tape contains the binary representation of x and its head is on the rightmost symbol of x. The second tape contains the binary representation of y and its head is on the rightmost symbol of y. The third...

  • Hi, I have Python programming problem as follow: Thank you. Best Regards. Write a function solution...

    Hi, I have Python programming problem as follow: Thank you. Best Regards. Write a function solution that, given two integers A and B, returns a string containing exactly A letters 'a' and exactly B letters 'b' with no three consecutive letters being the same in other words, neither "aaa" nor "bbb" may occur in the returned string). Examples: 1. Given A = 5 and B = 3, your function may return "aabaabab". Note that "abaabbaa" would also be a correct...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • In PYTHON: Write a function that receives a list of integers and returns the number of...

    In PYTHON: Write a function that receives a list of integers and returns the number of integers that are larger than the element immediately before them. For example, given [1,2,3,-5,0,-5,-10] the function should return 3 (since 1<2, 2<3, and -5<0).

  • PYTHON 3!!!!! Write a function: class Solution { public String solution(String T); } that, given a...

    PYTHON 3!!!!! Write a function: class Solution { public String solution(String T); } that, given a string T, returns the latest valid time that can be obtained from T, as a string in the format "HH:MM", where HH denotes a two-digit value for hours and MM denotes a two-digit value for minutes. Examples: 1. Given T = "2?:?8", the function should return "23:58". 2. Given T = "?8:4?", the function should return "18:49". 3. Given T = "??:??", the function...

  • 1.3 Write a function to output an arbitrary double number (which might be negative using only...

    1.3 Write a function to output an arbitrary double number (which might be negative using only printDigit for VO Write a recursive function that returns the number of 1 in the binary representation 1 in the representation of N/2 plus 1, if N is odd

  • Use C++ to write a recursive function that takes in a non-negative integer and returns the...

    Use C++ to write a recursive function that takes in a non-negative integer and returns the count of the occurrences of 7 as a digit, so for example 717 yields 2. Note: Even though it would be easy to solve this problem iteratively, the goal is to get some practice solving problems recursively.

  • Write a function that when given two integers, x and y, returns the maximum divisor of...

    Write a function that when given two integers, x and y, returns the maximum divisor of x or y, excluding x or y themselves. For example, if the two integers are 8 and 9, the divisors of 8 (excluding 8) are 1, 2, and 4 while the divisors of 9 (excluding 9) are 1 and 3. The maximum is 4. JAVA LANGUAGEEE!!!!

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT