An integer overflow (or wraparound) occurs when a number larger than the max integer value or smaller than the min integer value is tried to be stored in the integer's memory. You could imagine a circular number line.
Ex: If you have an integer at the max positive value and add one, then the integer will wraparound and be set to the most negative value (if using a signed int. An unsigned int will wraparound to 0). And, vise-versa.
Today, I experienced a bug where I wanted to get a (signed) integer overflow, but unfortunately the int value was just going to zero instead of going to the negative numbers. Thankfully, I knew the memory/bit representation of int and was able to figure out what was going on.
Here is some simplified code, with only one small change:
// Test #1 - has wraparound
int wrap = 1;
for (int i = 0; i < 34; i++) {
print(i + ": wrap: " + wrap);
wrap = wrap * 3;
}
// Test #2 - no wraparound
int wrap = 1;
for (int i = 0; i < 34; i++) {
print(i + ": wrap: " + wrap);
wrap = wrap * 2;
}
Here is the relevant output from running the code above:
// Test #1 - has wraparound
...
13: wrap: 1594323
14: wrap: 4782969
15: wrap: 14348907
16: wrap: 43046721
17: wrap: 129140163
18: wrap: 387420489
19: wrap: 1162261467
20: wrap: -808182895
21: wrap: 1870418611
22: wrap: 1316288537
23: wrap: -346101685
24: wrap: -1038305055
25: wrap: 1180052131
26: wrap: -754810903
27: wrap: 2030534587
28: wrap: 1796636465
29: wrap: 1094942099
30: wrap: -1010140999
31: wrap: 1264544299
32: wrap: -501334399
33: wrap: -1504003197
// Test #2 - no wraparound
...
20: wrap: 1048576
21: wrap: 2097152
22: wrap: 4194304
23: wrap: 8388608
24: wrap: 16777216
25: wrap: 33554432
26: wrap: 67108864
27: wrap: 134217728
28: wrap: 268435456
29: wrap: 536870912
30: wrap: 1073741824
31: wrap: -2147483648
32: wrap: 0
33: wrap: 0
If test #1 were to continue, then the values would keep wrapping. If test #2 were to continue, then the values would stay the same at 0.
The easiest way for me to make sense of this experiment was to know that multiplying by a power of 2 (2, 4, 8, 16, 32..) is the exact same as a bit shift in the binary computer. And, the bits that made up the int were all just getting pushed out of memory in test #2 until there were no more set bits to manipulate. In test #1, more int memory bits are being flipped in order to continue the wraparound.
I would explain this at a lower level, but there are already many great resources for "bit representation of integer".
~ Danial Goodwin ~
ps - This post was mainly for myself and written quickly. If there are any questions about this, then I'd be happy to elaborate.
Showing posts with label experiment. Show all posts
Showing posts with label experiment. Show all posts
2014-06-18
2013-07-11
Idea: Delete All Your Music
(Do you like discovering new things? Or do you feel that you are in a musical rut?)
Well, I love music and have a playlist for every occasion and in just about every genre.
Or so I think...
This next experiment has three steps:
1. The first is "deleting" all your music off your computer. (When I say delete, you can just move them or choose not to access them.) This way you aren't temped as easily to just go back to the old ways.
2. Forget about all of your favorite artists. You are biased, so you can try to limit your biases.
3. Search online for "Random Music" or "Music discovery service". The later is probably better if you feel that you are picky about your music.
Try this out for a week. And I'm sure you'll be pleasantly surprised with the results and the great new artists and music discover services that you find.
~ Simply Advanced ~
ps - In order to keep my biases out of the process, I decided to keep my favorite music discovery services off this post. If you would like to know them, then please feel free to message me and add me on Google Plus. On non-mobile screens, there is a link somewhere on the right side of the page.
Well, I love music and have a playlist for every occasion and in just about every genre.
Or so I think...
This next experiment has three steps:
1. The first is "deleting" all your music off your computer. (When I say delete, you can just move them or choose not to access them.) This way you aren't temped as easily to just go back to the old ways.
2. Forget about all of your favorite artists. You are biased, so you can try to limit your biases.
3. Search online for "Random Music" or "Music discovery service". The later is probably better if you feel that you are picky about your music.
Try this out for a week. And I'm sure you'll be pleasantly surprised with the results and the great new artists and music discover services that you find.
~ Simply Advanced ~
ps - In order to keep my biases out of the process, I decided to keep my favorite music discovery services off this post. If you would like to know them, then please feel free to message me and add me on Google Plus. On non-mobile screens, there is a link somewhere on the right side of the page.
2012-11-10
Windows 8 on a Mac
When people want Windows OS on a Mac they may think of dual booting or using a program like VMWare, VirtualBox, BootCamp, or Parallels to run the two simultaneously.
But, my roommate (Spence) and I experimented with another method to run Windows 7 and 8 on a Mac. We would replace the Mac's harddrive with the harddrive running Windows OS. Both were 2.5 inch.
What got us thinking on this track was that, first of all, the Mac wouldn't boot after being dropped. Us being computer savvy individuals, we didn't have another Mac lying around, but we did have an older Lenovo laptop that we thought we could possibly salvage parts from.
We took out the Mac harddrive, took out the Lenovo harddrive with Windows 7 and 8 dual booted, and swapped them. The process took less than ten minutes to figure out and complete.
The results?
Windows 8 runs beautifully on a 2009 aluminum uni-body Mac. Both Modern UI and desktop versions supported. And we were surprised that the Wi-Fi was easily connected to without installing any additional drivers.
The problems?
Initially, the trackpad only partially works (movement only, no clicking), but an external mouse does work flawlessly. Clicking and multitouch were added by installing the proper drivers for them. There may be a few more issues, but this is just a preliminary rundown of our experimental harddrive organ transplant.
If you also do a harddrive transplant, let us know! You can download some of Apple's drivers [at your own risk] from http://www.2shared.com/complete/J5_Inbmt/Apple_folder_from_Boot_Camp_Wi.html.
~ Simply Advanced ~
But, my roommate (Spence) and I experimented with another method to run Windows 7 and 8 on a Mac. We would replace the Mac's harddrive with the harddrive running Windows OS. Both were 2.5 inch.
What got us thinking on this track was that, first of all, the Mac wouldn't boot after being dropped. Us being computer savvy individuals, we didn't have another Mac lying around, but we did have an older Lenovo laptop that we thought we could possibly salvage parts from.
We took out the Mac harddrive, took out the Lenovo harddrive with Windows 7 and 8 dual booted, and swapped them. The process took less than ten minutes to figure out and complete.
The results?
Windows 8 runs beautifully on a 2009 aluminum uni-body Mac. Both Modern UI and desktop versions supported. And we were surprised that the Wi-Fi was easily connected to without installing any additional drivers.
The problems?
Initially, the trackpad only partially works (movement only, no clicking), but an external mouse does work flawlessly. Clicking and multitouch were added by installing the proper drivers for them. There may be a few more issues, but this is just a preliminary rundown of our experimental harddrive organ transplant.
If you also do a harddrive transplant, let us know! You can download some of Apple's drivers [at your own risk] from http://www.2shared.com/complete/J5_Inbmt/Apple_folder_from_Boot_Camp_Wi.html.
~ Simply Advanced ~
Subscribe to:
Posts (Atom)