Quote:
Originally posted by rwaliany
" (11111110) and add 1 (11111111). This can either be interpreted as 255 or -1." Only in PBASIC
Rbayer, notice "Sorry, my question was for C++ (or binary standard)," "how signs are stored in binary."
|
This is the same for either C or PBASIC. In order to differentiate between 255 and -1, you have to tell the compiler whether you are using a char or an unsigned char. If it's a char, it will interpret it as -1. If it's an unsigned char, it will be 255. Try this:
int main(){
int myNum=-1;
printf("Signed: %d\nUnsigned: %u\n", myNum, myNum);
}
You'll see that when interpreted as signed, it prints -1, as expected. When interpreted as unsigned, it will print 4294967295, which is the largest possible unsigned int (32 1's).
Using a similar program, you can find that -32768 is actually represented as 4294934528, which is 11111111111111111000000000000000.
32768=1000000000000000.
Invert: 0111111111111111.
Add 1: 1000000000000000.
Sign extend to 32-bits: 11111111111111111000000000000000, as expected.