View unanswered posts | View active topics It is currently Thu May 23, 2013 4:57 am






Reply to topic  [ 2 posts ] 
RobotC 8.23 firmware fails with unsigned values 
Author Message
Rookie

Joined: Fri Mar 21, 2008 8:00 pm
Posts: 9
Post RobotC 8.23 firmware fails with unsigned values
I have a problem with the unsigned values that in previouse version works well:

task main()
{
nxtDisplayTextLine(1, "%d",255);
while (true);
}

This code shows in the NXT window: 1680147487

and with this code:

task main()
{
byte a=255;
nxtDisplayTextLine(1, "%i",(unsigned int)a);
while (true);
}

The result is: -1


Tue Jan 29, 2013 12:36 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 537
Post Re: RobotC 8.23 firmware fails with unsigned values
The compiler is calculating this correctly. a "byte" is the same as a "char" which has a range of -128 to +127. When you assign a value of 255, it will overflow and end up with a value of -1.

Here is proof from a GCC compiler which exhibits the same behavior. http://i.imgur.com/BQQ7yQZ.png

Try using a ubyte instead which has a range of 0-255 and will be able to store your value.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Wed Jan 30, 2013 10:23 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.