View unanswered posts | View active topics It is currently Sat Aug 18, 2018 2:52 am

 Page 1 of 1 [ 13 posts ]
 Print view Previous topic | Next topic
Data Types
Author Message
Senior Roboticist

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Data Types
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-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.

Tue Apr 03, 2012 9:07 am
Moderator

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 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.

_________________
sudo rm -rf /

Tue Apr 03, 2012 12:54 pm
Senior Roboticist

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Re: Data Types
@magicode, @miki: Thanks, guys! That's exactly what I was looking for!

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

Tue Apr 03, 2012 1:22 pm
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Re: Data Types
 miki wrote:...Remarks:Never mix signed and unsigned data if you want to compare them with > or

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
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 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?

thx

_________________
leonoverweel.com

Tue Apr 03, 2012 5:45 pm
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Re: Data Types
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).

_________________
Matt

Tue Apr 03, 2012 6:23 pm
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Re: Data Types
Oh ok thanks!

_________________
leonoverweel.com

Tue Apr 03, 2012 6:53 pm
Moderator

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Re: Data Types
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.

_________________
sudo rm -rf /

Sun Oct 21, 2012 3:33 am
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: Data Types
I have prepared a slide deck that covers this topic for teaching our students on C. Here is a link to it.
http://proj.titanrobotics.net/docs/Clas ... onToC.pptx

Sun Oct 21, 2012 5:54 am
Novice

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
Re: Data Types
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.

 Code:typedef union {   byte bitsNumber;   struct    {      unsigned short bit1:1, bit2:1, bit3:1, bit4:1, bit5:1, bit6:1, bit7:1, bit8:1;   } bits;} bitSet;

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
Rookie

Joined: Mon Oct 22, 2012 2:19 am
Posts: 1
Re: Data Types

Mon Oct 22, 2012 2:50 am
Novice

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 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
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 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.

Fri Oct 26, 2012 3:29 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 13 posts ]

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning