We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
ok I'll try - see Int has usually 64bits - but let's make things easier and assume that we have only 4bits - one of these indicates positive or negative so we have 3bits for positive left
Now when you do 1(=001)*2(=010)*3(=011)*4(=100) you already are at 24(=11000) and as you can see this is more that fits the 4 bits (the same is true for 64bits - it just takes more factors)
if you get no overflow protection the higher bits are just truncated (and you will shift out all 1s and end up as GHCi tells you with 0).
On the other hand, if there is protection enabled you get an exception - in both cases you end up with a wrong program
if you switch to Integer the numbers can grow as big as you've got memory for your program (more than enough for this case) and so you don't run into overflows or exceptions here.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Huge GCD
You are viewing a single comment's thread. Return to all comments →
ok I'll try - see Int has usually 64bits - but let's make things easier and assume that we have only 4bits - one of these indicates positive or negative so we have 3bits for positive left
Now when you do 1(=001)*2(=010)*3(=011)*4(=100) you already are at 24(=11000) and as you can see this is more that fits the 4 bits (the same is true for 64bits - it just takes more factors)
if you get no overflow protection the higher bits are just truncated (and you will shift out all 1s and end up as GHCi tells you with 0).
On the other hand, if there is protection enabled you get an exception - in both cases you end up with a wrong program
if you switch to Integer the numbers can grow as big as you've got memory for your program (more than enough for this case) and so you don't run into overflows or exceptions here.