Getting started on the math class

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting started on the math class

Kasperkpedersen
Hi community. I have started on the module 12 and particularly the math class. I struggle with putting pseudocode presented in the book and lectures on coursera into actual jack programs. I understand the algorithms conceptually but find it difficult to see how to implement it.

Particularly i don't know how to get started on the bit(x,i) helper function and how to use it in the multiplication method.

What i have so far is this attempt to implement multiplication. The bit function and array of PowersOfTwo

class Math {
    function int multiply(int a, int b) {
        var int x;
        var int y;
        var int i;
        var int sum;

        let sum = 0;
        let i = 0;
        let x = x;
        let y = y;

        ith_bit_y = bit(y, i)

        if (ith_bit_y = 1) {
            sum = sum + x
            i = i + 1
        }
        return sum;
    }

I have also written the init method that initialize the array

    function void init() [
        var int n;
        static Array powersOfTwo;
    ]

Reply | Threaded
Open this post in threaded view
|

Re: Getting started on the math class

pmk21
Have another look at your pseudo code (from the book) to Jack translation.  You'll want the functionality of bit() to be within the loop.  For the long multiplication translation, it is pretty much 1-to-1 with the major difference the IF clause on how you'll want to check for the j-th bit being set (using the available bitwise operators).  Since you mentioned building the PowerOf2 array, how can you make use of that static array to help in the IF computation?
Reply | Threaded
Open this post in threaded view
|

Re: Getting started on the math class

Kasperkpedersen
Thank you for the response. I see that the pseudo code in the book has a if statement within a loop. I don't know how to create this while loop that i suppose is needed. The pseudo in the book has some value n that is the number of bits in x and y. I can see how if you could compare i against n, where it is the number of bits in y it could work. But how do one find the number of bits in y, that seems like another challenge that is not spoken about in the course.

Anyways if we say that i figure out how to write a while statement, then inside that i need to use the bit() function to perform a AND operation on the jth bit of y and the number j^2. The static array powersOfTwo may hold some values that corresponds to 1,2,4,8 and so on which if Y is for example 0110 it will compare 1 against 0, the second bit and so fourth. I can't really see the implementation for me both in terms of the while loop and how this bit function will make the correct evaluation..


Reply | Threaded
Open this post in threaded view
|

Re: Getting started on the math class

WBahn
Administrator
Kasperkpedersen wrote
Thank you for the response. I see that the pseudo code in the book has a if statement within a loop. I don't know how to create this while loop that i suppose is needed. The pseudo in the book has some value n that is the number of bits in x and y. I can see how if you could compare i against n, where it is the number of bits in y it could work. But how do one find the number of bits in y, that seems like another challenge that is not spoken about in the course.
You know what the number of bits in both numbers are. All of the numbers are sixteen-bit two's complement integers. You don't need to worry about the minimum number of bits needed to represent the particular number you are working with at the moment, just treat all numbers as having sixteen bits. If some of the numbers have some leading zeros (if positive) or ones (if negative), so what?