Memory layout of a data structure: Difference between revisions

m
whitespace
({{omit from|PARI/GP}})
m (whitespace)
Line 61:
Ring_Indicator at 0 range 8..8;
end record;</lang>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
{{wont work with|ALGOL 68G|Any - tested with release [https://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.15-0.8b/algol68g-mk15-0.8b.fc9.i386.rpm/download algol68g-1.15-0.8b] - bug is order of BITS}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
 
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
<lang algol68>MODE RSTWOTHREETWO = BITS;
Line 106:
unsigned clear_to_send : 1;
unsigned ring_indicator : 1;
};</lang>
</lang>
The ":1" gives the number of allocated bits. For unused bits (e.g. pin 11 in the 25-pin version above) the field name can be omitted.
 
Since as stated before the order of bits can't be assured '''but''' it could be important if we need to interact with hardware, the best way is to define ''bit masks''; of course actual writing/reading to/from an hardware "register" greater than a single byte must be done taking care of endianness.
 
{{omit from|Clojure}}
 
=={{header|D}}==
Implementation uses tango's BitArray structure.
 
{{libheader|tango}}
<lang D>module controlFieldsInStruct;
module controlFieldsInStruct;
 
import tango.core.BitArray;
Line 189 ⟶ 184:
 
return 0;
}</lang>
}
</lang>
 
Output:
Line 200 ⟶ 194:
Low level hardware control is a typical use of Forth. None of this is standard, however, since hardware I/O mechanisms differ on different systems. Forth does not have a structure mechanism, much less bitfields. These would be represented instead via bitmask constants if doing real serial port control.
 
<lang forth> : masks ( n -- ) 0 do 1 i lshift constant loop ;
9 masks DCD RxD TxD DTR SG DSR RTS CTS RI</lang>
 
Example usage, assuming I/O primitives '''in''' and '''out''':
 
<lang Tclforth> hex
hex
3fd constant com1-ctrl
decimal
Line 229 ⟶ 223:
wait-rx
2/
loop drop ;</lang>
Of course, this is a very simplified view of the full RS-232 protocol. Also, although this represents the order of the pins in a D-9 connector, this would not necessarily be the same as the order of the bits in a control register.
 
=={{header|J}}==
 
J does not support "structures", nor "fields in a structure". Instead, J supports arrays. And, of course, J could have labels corresponding to the elements of an array representing the state (voltage, current, logical bit value, whatever) of each pin of a 9-pin RS-232 plug:
<lang j>labels=: <;._2]0 :0
Line 286 ⟶ 279:
[1]
[1]</lang>
 
 
=={{header|OCaml}}==
'''Library:''' [http://code.google.com/p/ocaml-extlib/ extlib]
<lang ocaml>open ExtLib
open ExtLib
class rs232_data = object
val d = BitSet.create 9
Line 315 ⟶ 306:
method set_ring_indicator b = (if b then BitSet.set else BitSet.unset) d 8
end
;;</lang>
</lang>
 
=={{header|Perl}}==
<lang perl>use Bit::Vector::Minimal qw();
use Bit::Vector::Minimal qw();
my $vec = Bit::Vector::Minimal->new(size => 24);
 
Line 349 ⟶ 338:
 
$vec->set($rs232{'RD Received data'}, 1);
$vec->get($rs232{'TC Transmit clock'});</lang>
</lang>
 
=={{header|PicoLisp}}==
Line 369 ⟶ 357:
The ctypes module allows for the creation of Structures that can map between the structures of C and python datatypes. Within Structures, [http://docs.python.org/library/ctypes.html#bit-fields-in-structures-and-unions bit fields] can be created.
 
<lang python>from ctypes import Structure, c_int
from ctypes import Structure, c_int
 
rs232_9pin = "_0 CD RD TD DTR SG DSR RTS CTS RI".split()
Line 382 ⟶ 369:
class RS232_25pin(Structure):
_fields_ = [(__, c_int, 1) for __ in rs232_25pin]</lang>
 
</lang>
 
=={{header|Ruby}}==
Line 444 ⟶ 429:
=={{header|Tcl}}==
This Tcl implementation represents the fields as bits in an integer. It provides two functions to get from symbolic pin names to the integer, and vice versa.
<lang tcl>set rs232_bits {CD RD TD DTR SG DSR RTS CTS RI}
<lang Tcl>
set rs232_bits {CD RD TD DTR SG DSR RTS CTS RI}
 
proc rs232_encode args {
Line 473 ⟶ 457:
catch $test res
if {$res ne $expected} {puts "$test -> $res, expected $expected"}
}</lang>
}
</lang>
 
{{omit from|AWK}}
{{omit from|Batch File|No memory management or data structures.}}
{{omit from|Clojure}}
{{omit from|E}}
{{omit from|gnuplot}}
Anonymous user