Byte operations

Hitec robotics including ROBONOVA humanoid, HSR-8498HB servos, MR C-3024 Controllers and RoboBasic
7 postsPage 1 of 1
7 postsPage 1 of 1

Byte operations

Post by RoboJeronimo » Wed Aug 05, 2009 5:57 pm

Post by RoboJeronimo
Wed Aug 05, 2009 5:57 pm

Hi,

What are the result of...:?:

DIM var AS BYTE 'max value=255 isn't it?
var=255
var=var+3
-------------------------------------------------------
DIM var AS BYTE 'min value=0 isn't it?
var=0
var=var-3
Hi,

What are the result of...:?:

DIM var AS BYTE 'max value=255 isn't it?
var=255
var=var+3
-------------------------------------------------------
DIM var AS BYTE 'min value=0 isn't it?
var=0
var=var-3
RoboJeronimo offline
Robot Builder
Robot Builder
Posts: 19
Joined: Fri Jul 10, 2009 11:54 am

Post by NovaOne » Wed Aug 05, 2009 8:17 pm

Post by NovaOne
Wed Aug 05, 2009 8:17 pm

DIM var AS BYTE
var=255
var=var+3

So var = 2

DIM var AS BYTE
var=0
var=var-3

So var = 253

Is this wrong?
DIM var AS BYTE
var=255
var=var+3

So var = 2

DIM var AS BYTE
var=0
var=var-3

So var = 253

Is this wrong?
NovaOne offline
Savvy Roboteer
Savvy Roboteer
Posts: 405
Joined: Thu Jul 05, 2007 7:30 am

Post by RoboJeronimo » Wed Aug 05, 2009 8:52 pm

Post by RoboJeronimo
Wed Aug 05, 2009 8:52 pm

So the secuencie is:

...253 - 254 - 255 - 0 - 1 - 2 - 3...
So the secuencie is:

...253 - 254 - 255 - 0 - 1 - 2 - 3...
RoboJeronimo offline
Robot Builder
Robot Builder
Posts: 19
Joined: Fri Jul 10, 2009 11:54 am

Post by TheJuggler » Thu Aug 06, 2009 2:19 am

Post by TheJuggler
Thu Aug 06, 2009 2:19 am

That's all assuming that the byte is unsigned.

If it's signed then when you add 3 to 255, you'll get a negative number, and likewise when you subtract 3 from 0 you'll get -3. (although 255 isn't really possible since there are only 7 bits available for data meaning that you can get a max of 127.)

But if it's signed, then the maximum value is 127, as the most significant bit will be the sign bit.
That's all assuming that the byte is unsigned.

If it's signed then when you add 3 to 255, you'll get a negative number, and likewise when you subtract 3 from 0 you'll get -3. (although 255 isn't really possible since there are only 7 bits available for data meaning that you can get a max of 127.)

But if it's signed, then the maximum value is 127, as the most significant bit will be the sign bit.
Last edited by TheJuggler on Thu Aug 06, 2009 6:17 pm, edited 1 time in total.
TheJuggler offline
Robot Builder
Robot Builder
Posts: 13
Joined: Mon Jul 20, 2009 2:07 pm

Post by l3v3rz » Thu Aug 06, 2009 10:40 am

Post by l3v3rz
Thu Aug 06, 2009 10:40 am

A byte is usually considered to be unsigned 8 bit number i.e. 0-255.

If you have an 8 bit signed integer the actual number range is -128 to 127.
A byte is usually considered to be unsigned 8 bit number i.e. 0-255.

If you have an 8 bit signed integer the actual number range is -128 to 127.
l3v3rz offline
Savvy Roboteer
Savvy Roboteer
Posts: 473
Joined: Fri Jul 18, 2008 2:34 pm

Post by RoboJeronimo » Mon Aug 10, 2009 7:26 pm

Post by RoboJeronimo
Mon Aug 10, 2009 7:26 pm

I check it again, with SERVO instruction.

var=255+3
SERVO 6, var

The variable is circular
I check it again, with SERVO instruction.

var=255+3
SERVO 6, var

The variable is circular
Last edited by RoboJeronimo on Tue Aug 11, 2009 6:30 pm, edited 1 time in total.
RoboJeronimo offline
Robot Builder
Robot Builder
Posts: 19
Joined: Fri Jul 10, 2009 11:54 am

Post by i-Bot » Mon Aug 10, 2009 10:34 pm

Post by i-Bot
Mon Aug 10, 2009 10:34 pm

Even though a BYTE variable, Robobasic first does the maths at 16 bits, so 255(0000 0000 1111 1111b) + 3(0000 0000 0000 00011b) = 258(0000 0001 0000 0010b). This is truncated to 8 bits when the variable is stored again as a BYTE, giving the value 2(0000 0010b).

For the subtract 0(0000 0000 0000 0000b) - 3(0000 0000 0000 0010) = 65533( C 1111 1111 1111 1101) note carry bit is set. This is truncated and carry bit ignored to give a BYTE of 253(1111 1101).

How did you check it ? the compiler may limit input values to 0 to 255, but the above is the way the maths is done when it is executed.
Even though a BYTE variable, Robobasic first does the maths at 16 bits, so 255(0000 0000 1111 1111b) + 3(0000 0000 0000 00011b) = 258(0000 0001 0000 0010b). This is truncated to 8 bits when the variable is stored again as a BYTE, giving the value 2(0000 0010b).

For the subtract 0(0000 0000 0000 0000b) - 3(0000 0000 0000 0010) = 65533( C 1111 1111 1111 1101) note carry bit is set. This is truncated and carry bit ignored to give a BYTE of 253(1111 1101).

How did you check it ? the compiler may limit input values to 0 to 255, but the above is the way the maths is done when it is executed.
i-Bot offline
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 1142
Joined: Wed May 17, 2006 1:00 am


7 postsPage 1 of 1
7 postsPage 1 of 1
cron