Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Data Type Usage
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mackie




PostPosted: Thu Apr 10, 2008 11:18 pm   Post subject: [Tutorial] Data Type Usage

Data Types

As requested, I'm going to go over some of the basics of data types that Turing offers. Now why do you care? Because if you want your programs to be efficient and work at top notch speed you need to know how to manage memory.

What is a data type?

A data type is template for an allocation of memory. Basically when you make a variable, that variable has a certain amount of the computers RAM, or memory, assigned to it. You can use no more, or less of that amount. Think of it like a bucket, a bucket will take up the same amount of space in your garage, no matter what you put inside of it.

Goal?

The whole point of this lesson is to teach you how to better use memory, to speed up your programs. Even though it's not much use in Turing, since Turing is already so slow, it will help in later programming endeavors I'm sure.

Bits and Bytes

Now, everybody knows what a bit and a byte are right? Well... Here it is anyway.

Bits stand for binary digits, basically just a fancy way of saying it is a switch set to 1 or 0.

A byte is 8 bits.
A kilobyte is 1024 bytes.
A megabyte is 1024 kilobytes.
A gigabyte is 1024 megabytes.

And so on... The less the better! Everything in the digital domain is a combination of 1's or 0's if you didn't know that already. It is how we measure memory usage, among other things.

The Basics

Data types are like onions, well no, actually they're not. They're more like data types actually. So most folks learn a few basic types to start out with.

Integer: Used to store positive of negative whole numbers.

Real/Double/Float: Used to store positive or negative numbers with decimals, it can also hold larger whole numbers than integers.

String: Used to store 'strings of characters' or simply just text.

Boolean: Used to store true of false values. (1 or 0)


The Details

Integers:

Again they are used to store whole numbers. They have a range of -2147483648 to 2147483647.

They are defined as such and use 4 bytes each.
Turing:
var foobar : int


How can we make these smaller Mackie!

Well Turing actually lets you specify how many, bits you want to be used per integer. You can declare then as any of the following: int1, int2, int4.

The number indicates how many byes are being used. Just like to mention that int4 is (almost) identical to int.



Boolean:

Booleans are always 1 byte. Why not a bit? I have no clue.
Turing:
var foobar : boolean




Real:
Reals are also called, floating point numbers, or doubles. They are used to store any numbers between -10e308 and +10e308 with decimals of course.

They are defined as such and use 8 bytes each.
[syntax"turing"]var foobar : real [/syntax]

How can we make these smaller Mackie!

The alternatives to real include, real4 and real8. Reals should be avoided unless absolutely necessary. They are very costly to memory usage.



Natural:

Natural numbers range from 0 to 4294967294. They are can't hold decimals and are very useful when you don't have to go into the negatives

They are defined as such and use 4 bytes each.
Turing:
var foobar : nat


How can we make these smaller Mackie!

The alternatives to nat include, nat1, nat2, and nat4. These can be very useful.



String:

Alright, strings are the most costly variable you'll run into. They take up 1 byte for every character allocated. By default 256 character(0 to 255) spaces are available. Thats 1 kilobyte for every 4 strings. :/. This needs to be taken down to size, but first off.


They are defined as such and use 256 bytes each.
Turing:
var foobar : string


How can we make these smaller Mackie!

Hear is how you can set the size of your string!
Turing:
var foobar : string(30)


You simply enter the number of characters you want in the brackets beside the data type. You can save on tons of space this way!



Constants:

Constants are nifty things. They only allocate as much space as they take up, so if you have the word ?hello? it will only take up 5 bytes. If you have values that stay the same during the entire program you should try your hardest to keep them Constant.

Turing:
const FOOBAR := 50


Constants can hold any type of data.



There you go.

I hope you learned something. I sure did. Just remember that premature optimization is the root of all evil. Well.. That and MihaiG, but lets wait for the 4th comic for that one. Razz
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: