APLX Help : Help on APL language : System Functions & Variables : ⎕DR Data Representation




The system function 1 = Boolean (patterns of 1s and 0s) 2 = Integer 3 = Floating point 4 = Character 5 = Overlay 6 = Nested or mixed 7 = Object or class reference Oneargument formReports data type of any array. ⎕DR 2.9 3 X←1 0 1 1 0 1 ⎕DR X 1 ⎕DR 'ABC' 1 2 3 6 ⎕DR (⍳10) (2 2⍴⍳4) 6 APL will choose which type of number format to use, and certain operations will force data to be of a specific type. For example, the result of a comparison Code Type Space ─────────────────────────────────────────────── 1 Boolean 1 bit per element 2 Integer 4 bytes per element (32 bits) or 8 bytes per element (64 bits) under APLX64 3 Floating point 8 bytes per element (64 bits) 4 Character 1 byte per element (8 bits) 5 Overlay see ⎕OV 6 Nested/mixed depends on contents 7 Object/Class ref. 4 bytes per element (32 bits) or 8 bytes per element (64 bits) under APLX64 (see also Twoargument form (scalar left argument in range 1 to 4)On occasions it is useful to examine or change the data type, either for encryption purposes, or to combine character and numeric data quickly. When used with a left argument consisting of one of the datatype codes in the range 1 to 4, and a right argument consisting of a data array of type 1 to 4, 1 ⎕DR 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 (The 32bit binary pattern for 5) Note that, under APLX64, integers are represented internally as 64bit numbers, so converting an integer scalar to binary returns a length64 binary vector. The conversion of one type to another changes the number of elements in data, and so How the data conversion worksIn essence, all that dyadic Suppose you start with the character vector '1234'. This data is held internally as the four bytes, hex 31 32 33 34. If those same four bytes are used as the data portion of a binary vector, you will get the bit pattern corresponding to those four bytes: 1 ⎕DR '1234' 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 0 The first 8 bits are the binary pattern for hex 31, i.e. 0 0 1 1 0 0 0 1, the second 8 bits are the pattern for hex 32, etc. If you represent this data as an integer (in a 32bit version of APLX), you will get the 32bit integer which corresponds to this bit pattern: 2 ⎕DR '1234' 825373492 which is the decimal number equivalent to hex 31323334. Notesa) For a scalar or vector, the length obviously changes (a 32bit scalar integer becomes a length four character vector or a length 32 binary vector). For higher dimensional arrays, the last dimension is increased or reduced as necessary. b) But what if there are not enough elements? For example, suppose we
ask for the 8byte float representation but only give it 4 bytes (this
would happen if we ask for c) Changing arbitrary data to float is potentially dangerous because you can produce a bit pattern which is not legal as a 64bit IEEE floating point number (the internal representation used by APLX for float numbers). d) If you are running APLX64, the 64bit version of APLX, you will get different answers for integer arguments, because integers are represented as 8 bytenumbers in APLX64. In APLX: 1 ⎕DR 825373492 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 0 ⍴1 ⎕DR 825373492 32 In APLX64: 1 ⎕DR 825373492 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 0 ⍴1 ⎕DR 825373492 64 ByteOrdering IssuesIn the case of a bigendian processor (PowerPC, SPARC, 68000, etc), the 32bit hex number 31323234, if viewed as a series of bytes, is in the order you expect: hex bytes 31 32 33 34. But on a littleendian processor (Intel), it is backwards: 34 33 32 31. So the question arises: on a littleendian processor, should the result of transforming a 32bit integer to binary/character treat the data as a 32bit container (hex 31323334), or as a series of four bytes as they would appear in memory (hex 34 33 32 31)? In APLX, by default it is treated as 32bit container (i.e. effectively swap the bytes for the littleendian case  but see below for changing this default). This design decision was taken for two reasons. Firstly, it means the result is the same on all APLX (32bit) platforms. Secondly, it means the results are consistent with what you would reasonably expect. For example, on all 32bit platforms APLX gives the following result when converting an integer to binary: 1 ⎕DR 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 (the bit pattern for the integer 2, comprising four bytes) ⎕AF 4 ⎕DR 2 0 0 0 2 (the individual bytes which make up the 4byte number 2, even though on a littleendian machine they would be backwards if the processor read them as individual bytes) 2 ⎕DR 4 ⎕DR 1 ⎕DR 2 2 (Convert the integer number 2 to binary. Convert the resulting binary vector to a length 4 character vector. You get back the number you started with.) Twoargument form (vector left argument or compatibility mode)As well as the default conversions described above, you can force alternative representations of the converted data by providing a two or threeelement vector as the left argument. You can also use alternative codes for the standard conversions 1 to 4 in the first element, for compatibility with other APL interpreters, as follows: Code Type Space ─────────────────────────────────────────────── 11 Boolean 1 bit per element 83 Integer 1 byte per element (8 bits) 163 Integer 2 bytes per element (16 bits, littleendian) 323 Integer 4 bytes per element (32 bits, littleendian) 7 Integer 8 bytes per element (64 bits) 643 Integer 8 bytes per element (64 bits, littleendian) 645 Floating point 8 bytes per element (64 bits, littleendian) 82 Character 1 byte per element (8 bits) The optional second element, if supplied, is the number of bytes per element when converting from character to integer or float, and vice versa. 0 means use the default value implied by the first argument. The third element, if supplied, is the byteordering (endian) flag: 0 Bigendian or as implied by the fist element (default) 1 Littleendian 2 Naturalendian (big on bigendian systems, little on littleendian systems) Note that you can only specify the element size if you are converting to or from a character representation. Examples⎕AF 4 ⎕DR 2 ⍝ Convert 32bit integer 2 to four characters 0 0 0 2 ⎕AF 4 0 1 ⎕DR 2 ⍝ Same, but littleendian byte order 2 0 0 0 ⎕AF 4 2 1 ⎕DR 2 ⍝ Force integer to be consider a 16bit value 2 0 ⎕AF 4 2 1 ⎕DR 200000 ⍝ Too large to fit in 16 bits DOMAIN ERROR ⎕AF 4 2 1 ⎕dr 200000 ^ ⎕AF 82 ⎕DR 2 ⍝ Compatibility mode: Int to Char, littleendian 2 0 0 0 323 ⎕DR 82 ⎕DR 23 ⍝ Round trip 23 ⎕AF 4 ⎕DR 2.56 ⍝ Convert float to 8 bytes (IEEE Double representation) 64 4 122 225 71 174 20 122 ⎕AF 4 4 ⎕DR 2.56 ⍝ Convert float to 4 bytes (IEEE Single representation) 64 35 215 10 

APLX Help : Help on APL language : System Functions & Variables : ⎕DR Data Representation

Copyright © 19962010 MicroAPL Ltd