911paramedic
Diamond Member
While my textbook is good, it sucks at explaining why things do what they do. I need some good sites to add to my reading when it comes to learning this.
Such as www.learncpp.com, I went there and they had a great example of what happens when you add one (+1) to the maximum allowed number:
My textbook pretty much just said, you would get an incorrect number. Great, why? Well the above site's explanation enabled me to (excuse the pun) wrap my head around it.
So, that's what I'm looking for, sites with decent explanations or tutorials.
Thanks.
EDIT: There is no hope, edited for bb code programming error.
Such as www.learncpp.com, I went there and they had a great example of what happens when you add one (+1) to the maximum allowed number:
#include <iostream.h>
int main()
{
using namespace std;
unsigned short x = 65535; // largest 2-byte unsigned value possible
cout << "x was: " << x << endl;
x = x + 1; // We desire 65536, but we get overflow!
cout << "x is now: " << x << endl;
}
What do you think the result of this program will be?
x was: 65535
x is now: 0
The number 65,535 is represented by the bit pattern 1111 1111 1111 1111 in binary. 65,535 is the largest number an unsigned 2 byte (16-bit) integer can hold, as it uses all 16 bits. When we add 1 to the value, the new value should be 65,536. However, the bit pattern of 65,536 is represented in binary as 1 0000 0000 0000 0000, which is 17 bits! Consequently, the highest bit (which is the 1) is lost, and the low 16 bits are all that is left. The bit pattern 0000 0000 0000 0000 corresponds to the number 0, which is our result!
My textbook pretty much just said, you would get an incorrect number. Great, why? Well the above site's explanation enabled me to (excuse the pun) wrap my head around it.
So, that's what I'm looking for, sites with decent explanations or tutorials.
Thanks.
EDIT: There is no hope, edited for bb code programming error.