ROBOTC.net forumshttp://www.robotc.net/forums/ Data Typeshttp://www.robotc.net/forums/viewtopic.php?f=1&t=4427 Page 1 of 1

 Author: NeXT-Generation [ Tue Apr 03, 2012 9:07 am ] Post subject: 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.

 Author: magicode [ Tue Apr 03, 2012 12:54 pm ] Post subject: 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.

 Author: NeXT-Generation [ Tue Apr 03, 2012 1:22 pm ] Post subject: Re: Data Types @magicode, @miki: Thanks, guys! That's exactly what I was looking for!

Author:  mattallen37 [ Tue Apr 03, 2012 3:40 pm ]
Post subject:  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.

 Author: DiMastero [ Tue Apr 03, 2012 5:45 pm ] Post subject: 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

 Author: mattallen37 [ Tue Apr 03, 2012 6:23 pm ] Post subject: 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).

 Author: DiMastero [ Tue Apr 03, 2012 6:53 pm ] Post subject: Re: Data Types Oh ok thanks!

 Author: magicode [ Sun Oct 21, 2012 3:33 am ] Post subject: 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.

 Author: MHTS [ Sun Oct 21, 2012 5:54 am ] Post subject: 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

Author:  amcerbu [ Sun Oct 21, 2012 10:42 pm ]
Post subject:  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.

 Author: VeraKeisey [ Mon Oct 22, 2012 2:50 am ] Post subject: Re: Data Types LOL... Thanks for your sharing,

Author:  amcerbu [ Fri Oct 26, 2012 12:39 am ]
Post subject:  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?

Author:  MHTS [ Fri Oct 26, 2012 3:29 am ]
Post subject:  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.

 Page 1 of 1 All times are UTC - 5 hours [ DST ] Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/