Diving a little deeper - breaking code

Hey guys; I paused at this section to test a few things to cement what we have already learned. After to experiment and ‘break’ it to understand a bit more.

One thing in particular you might interesting is the limit of “int”

So at a glance you might think it simply adds 1 to the ‘int Busted’- but instead it wraps around as shown:
experimentresult

I looked in a bit further on my own to discover why this happens:

“int”, as it turns out, creates a variable that is designated to a portion of your RAM. The size of this seems to be 32 bits (I believe this is determined by your computer; so a 64 bit machine would do 64)… to help illustrate what your computer is actually doing lets think of this in a 8 bit example.

int busted = 0 is read in your RAM as 0-0-0-0-0-0-0-0
int busted = 1 is 0-0-0-0-0-0-0-1
int busted = 255 is 1-1-1-1-1-1-1-1

In this case; when we add 1 to 255

int busted = busted+1 the readout becomes 0-0-0-0-0-0-0-0 or 0 : the wrap around effect

Now in my posted example; the reason it goes (-) negative; is because our C++ system has designated that ‘int’ can be both positive and negative; so it has split the 32-bit RAM in half for each side of 0.

In diving deeper; I discovered there is the ‘unsigned int’ - which doubles the positive values; but does not allow for negatives. (This could be important if something never goes negative; lets say we are counting a players gold in an RPG. - He never has ‘negative’ gold; therefore to allow him to carry more without using more RAM; we can use it as ‘unsigned int’

So anyway; at this point I’m curious what are you guys experimenting with.

Share you experiments and lets learn more!

Privacy & Terms