Hi! I've been using RobotC (intermittently) for about 6 months, and I believe that I understand most of the intermediate level stuff. But, there's one thing that's got me confused: Data Types. I don't understand the difference between, say, a signed int and a unsigned int or a normal int. What's the difference between a byte and a ubyte? So, could anyone give me an explanation of the data types, and what data they hold? Thanks!

P.S. I know how to use them, I just don't understand what exactly all that extra stuff is.

_________________A.K.A. inxt-generationSelf-proclaimed genius, and future world dominator.My Brickshelf Folder"Don't they teach recreational mathematics anymore?" - The Tenth DoctorBow down to Nikola Tesla, King of the Geek Gods.

Tue Apr 03, 2012 9:07 am

magicode

Moderator

Joined: Tue Sep 14, 2010 9:19 pmPosts: 496

Re: Data Types

Data types tell the computer how big something is (honestly, a Google search will explain better). I'm not exactly sure what values are used in ROBOTC, but for example: a long will be 32 bits, an int will be 16 bits, a char will be 8 bits. As for the NXT architecture itself, a word is 32 bits. The difference between signed and unsigned is whether you can tell if the number is positive or negative. Here's an example: Lets say an int is 16 bits. it looks something like this 1010101010101010. How many values can you have with 16 bits? 2^16 different values. So you can denote numbers from 0 - 65536. This is an unsigned int, where every number will be positive. But if you also want negative numbers, you have to have some way of telling if the number is positive or negative. There are a couple ways to do this. For example: the first bit can be used to tell if the number is positive or negative. this means you have only 15 bits to store the actual int data. that's 2^15 = 32,768 values. so your number range is from -32768 to 32768. Does that clear things up a bit?EDIT: see later post for added information on how the NXT actually stores the information.

_________________A.K.A. inxt-generationSelf-proclaimed genius, and future world dominator.My Brickshelf Folder"Don't they teach recreational mathematics anymore?" - The Tenth DoctorBow down to Nikola Tesla, King of the Geek Gods.

Tue Apr 03, 2012 1:22 pm

mattallen37

Expert

Joined: Thu Sep 29, 2011 11:09 pmPosts: 184Location: Michigan USA

Re: Data Types

miki wrote:

...Remarks:Never mix signed and unsigned data if you want to compare them with > or <Casting a signed data into unsigned data will not convert the data value....

Are those remarks specific to ROBOTC? I know in NXC you don't have to worry about either of those, and I don't think you do in C++ for Arduino either.

Actually, in regard to the second one, that is sort of true in NXC. You obviously can't set an unsigned int to the value of -5 (with the result of -5), but you can set an unsigned int to the value of a signed int with a positive value.

_________________Matt

Tue Apr 03, 2012 3:40 pm

DiMastero

Expert

Joined: Wed Jun 30, 2010 7:15 amPosts: 181

Re: Data Types

This is a bit unrelated but ties in: I read somewhere that a binary bit takes up 8 bits of space -- anyone know why that is? I mean it's only one bit of information to store so why does it take so much space?

You mean the bool data type? It's because RAM is like a huge array, addressable by byte, not bit.

You can however store bits in a more compact way. For example, you could use the byte type, and store 8 boolean values (use all 8 bits). However, you would need to use bit-math to convert it to 1s and 0s, which means it would add up to many more OPcodes (thus slower access speeds).

As I was searching for something, I came across and reread this thread. I realized that I implied in my previous post that the NXT actually stores integers with just a signed bit in the front. To clarify, this is probably not what happens. Now I don't know exactly how ROBOTC interacts with the NXT, but assuming it uses the hardware implementation of the ARM7 in the brick, signed integers would be stored using a method called two's complement. Using the method described in the above post can cause problems, like there being two zeros, and normal addition not working properly. Just thought that I'd clarify this point.

If I'm not mistaken, you can access individual bits with the following trick, and allow the compiler to handle the binary arithmetic. I'm not certain that it's implemented in RobotC, but I've used it before in C programs for the TI-89 graphing calculator (check out TIGCC). It'd be rare that you'd ever need to save memory like this, but hey, it's fun.

A union is a useful tool (which I do know exists in RobotC) that allows you to address the same block of memory in different ways. In this particular code example, an instance of type bitSet is either treated as a byte (8-bit unsigned integer) or as a struct. You can write values to the bitsNumber member, and the values of bit1, bit2, bit3, bit4, etc. will change; the same goes for modifying the values of bit1, bit2, etc.

The ":1" following each short integer declaration within the struct tells the compiler that each integer should only take up 1 bit in memory.

You access items in a union the same way you would in a struct: UnionName.MemberName.

Sun Oct 21, 2012 10:42 pm

VeraKeisey

Rookie

Joined: Mon Oct 22, 2012 2:19 amPosts: 1

Re: Data Types

LOL... Thanks for your sharing,

Mon Oct 22, 2012 2:50 am

amcerbu

Novice

Joined: Sun Oct 21, 2012 10:01 pmPosts: 76

Re: Data Types

Actually, I just tested out the code above... it gives a bunch of compiler errors (the :1 feature isn't implemented in RobotC, it seems).

Does anyone know what happens if you create a union between variables that don't occupy the same amount of space in memory? For example, how would this behave?

Code:

typedef union{ short shortInteger; long longInteger;} testUnion;

Does writing to shortInteger only change some of the bytes in longInteger? Can shortInteger be changed without affecting the value of longInteger? How large is each testUnion instance?

Fri Oct 26, 2012 12:39 am

MHTS

Guru

Joined: Sun Nov 15, 2009 5:46 amPosts: 1523

Re: Data Types

amcerbu wrote:

Actually, I just tested out the code above... it gives a bunch of compiler errors (the :1 feature isn't implemented in RobotC, it seems).

Does anyone know what happens if you create a union between variables that don't occupy the same amount of space in memory? For example, how would this behave?

Code:

typedef union{ short shortInteger; long longInteger;} testUnion;

Does writing to shortInteger only change some of the bytes in longInteger? Can shortInteger be changed without affecting the value of longInteger? How large is each testUnion instance?

Yes, writing to shortInteger will change part of longInteger. Regarding to how big is testUnion, it should be the larger of the two data types, so it should be 4 bytes long.

Who is online

Users browsing this forum: No registered users and 2 guests

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