Thread: PBASIC sucks
View Single Post
  #24   Spotlight this post!  
Unread 21-01-2003, 22:24
rbayer's Avatar Unsung FIRST Hero
rbayer rbayer is offline
Blood, Sweat, and Code
no team (Teamless Orphan)
 
Join Date: Mar 2002
Rookie Year: 2001
Location: Minnetonka, MN
Posts: 1,087
rbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of light
Send a message via AIM to rbayer
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.
__________________
New C-based RoboEmu2 (code simulator) available at: http://www.robbayer.com/software.php