integer value of 1000000000000000

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

integer value of 1000000000000000

Juan313
Hi, I'm trying to create the array twoToThe[j] whose jth location represent 2 to power of j.  What's the integer value for twoToThe[15]? It should be the integer representation of binary code 1 followed by 15 zeros. I thought it's -32768. However the compiler complains "Integer constant too big". What's the correct constant? Any help is greatly appreciated!

--Juan
Reply | Threaded
Open this post in threaded view
|

Re: integer value of 1000000000000000

cadet1620
Administrator
You are correct, the binary value 1000 0000 0000 0000 is -32768 in 2's-complement. But the Jack compiler can't handle it because the '-' is actually unary negation and 32768 is not a legal positive integer.

What you can do is 32767+1 or ~32767.

Also note that if you are using
    let twoToThe[j+1] = twoToThe[j]+twoToThe[j];
to initialize the array, 16384+16384 will result in -32768.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: integer value of 1000000000000000

Juan313
In reply to this post by Juan313
Thank you guys for the quick reply. I let twoToThe[15]= -32767-1. It's woking now. I also examined the RAM location. It's actually -32768. Thanks again for the help!