source: trunk/SRC/ToBeReviewed/LECTURE/inverse_binary.pro

Last change on this file was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1;+
2;
3; @file_comments
4; Inverse function of the <pro>binary</pro> function => given a
5; input array of 0/1, return its corresponding byte/integer/long
6; representation
7;
8; @categories
9;
10; @param BINNUMB {in}{required}
11; Must be a binary type array containing only 0 and 1.
12; According to <pro>binary</pro> outputs, binnum array must have the
13; following dimensions values: (8, t, d1, d2...)
14; t gives the output type: t = 1 -> byte
15;                             t = 2 -> integer
16;                             t = 4 -> long
17;
18;  (d1, d2...) are the output dimensions
19;
20; @returns
21; A byte/integer/long array with (d1, d2...) dimensions
22;
23; @restrictions
24; The binary number can represent only byte/integer/long
25;
26; @examples
27;
28;   IDL> a=indgen(5)
29;   IDL> b=binary(a)
30;   IDL> help, b
31;     B               BYTE      = Array[8, 2, 5]
32;   IDL> print, b
33;        0   0   0   0   0   0   0   0
34;        0   0   0   0   0   0   0   0
35;
36;        0   0   0   0   0   0   0   0
37;        0   0   0   0   0   0   0   1
38;
39;        0   0   0   0   0   0   0   0
40;        0   0   0   0   0   0   1   0
41;
42;        0   0   0   0   0   0   0   0
43;        0   0   0   0   0   0   1   1
44;
45;        0   0   0   0   0   0   0   0
46;        0   0   0   0   0   1   0   0
47;   IDL> help, inverse_binary(b)
48;     <Expression>    INT       = Array[5]
49;   IDL> print, inverse_binary(b)
50;            0       1       2       3       4
51;
52; @history
53;      Sebastien Masson (smasson\@jamstec.go.jp)
54;      July 2004
55;
56; @version
57; $Id$
58;
59;-
60FUNCTION inverse_binary, binnumb
61;
62  compile_opt idl2, strictarrsubs
63;
64  s = size(binnumb, /dimensions)
65  IF n_elements(s) EQ 1 THEN numbofbit = 8 ELSE numbofbit = 8*s[1]
66  nvalues = n_elements(binnumb)/numbofbit
67  bn = reform(long(binnumb), numbofbit, nvalues)
68;
69  CASE numbofbit OF
70    8:res = byte(total(temporary(bn)*2b^reverse(indgen(numbofbit)#replicate(1b, nvalues), 1), 1))
71    16:res = fix(total(temporary(bn)*2^reverse(indgen(numbofbit)#replicate(1, nvalues), 1), 1, /double))
72    32:res = long(total(temporary(bn)*2L^reverse(indgen(numbofbit)#replicate(1L, nvalues), 1), 1, /double))
73  ENDCASE
74;
75  CASE n_elements(s) OF
76    1:res = res[0]
77    2:res = res[0]
78    3:
79    ELSE:res = reform(res, s[2:n_elements(s)-1], /over)
80  ENDCASE
81;
82  return, res
83end
Note: See TracBrowser for help on using the repository browser.