Variable size/Get: Difference between revisions

From Rosetta Code
Content added Content deleted
(-> IDL and Tcl)
Line 7: Line 7:
Int_Bits : constant Integer := Integer'size;
Int_Bits : constant Integer := Integer'size;
Whole_Bytes : constant Integer := Int_Bits / Storage_Unit; -- Storage_Unit is the number of bits per storage element
Whole_Bytes : constant Integer := Int_Bits / Storage_Unit; -- Storage_Unit is the number of bits per storage element

==[[IDL]]==
[[Category:IDL]]

IDL is array based, so its <tt>size()</tt> function is geared towards that:

arr = intarr(3,4)
print,size(arr)
;=> prints this:
2 3 4 2 12

The result means: 2 dimensions in the array, the first dimension has extent 3, the second has extent 4, the elements of the array are 2-byte integers (IDL's default for an "int"), there's a total of 12 elements in the array.

==[[Tcl]]==
[[Category:Tcl]]

Since all variables are ultimately strings in Tcl, this is easy:

string bytelength $var

Revision as of 07:42, 27 February 2007

Task
Variable size/Get
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrate how to get the size of a variable.

Ada

Ada represents the size of a variable in bits, not bytes like many other languages.

Int_Bits : constant Integer := Integer'size;
Whole_Bytes : constant Integer := Int_Bits / Storage_Unit; -- Storage_Unit is the number of bits per storage element

IDL

IDL is array based, so its size() function is geared towards that:

arr = intarr(3,4)
print,size(arr)
;=> prints this:
       2           3           4           2          12

The result means: 2 dimensions in the array, the first dimension has extent 3, the second has extent 4, the elements of the array are 2-byte integers (IDL's default for an "int"), there's a total of 12 elements in the array.

Tcl

Since all variables are ultimately strings in Tcl, this is easy:

string bytelength $var