File: cgbitget.pro

package info (click to toggle)
coyote 2019.01.29-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,316 kB
  • sloc: python: 184; makefile: 14; sh: 13
file content (129 lines) | stat: -rw-r--r-- 5,877 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
; docformat = 'rst'
;
; NAME:
;   cgBitGet
;
; PURPOSE:
;   The function returns the bit value (0 or 1) of a specified bit in a supplied number.
;
;******************************************************************************************;
;                                                                                          ;
;  Copyright (c) 2014, by Fanning Software Consulting, Inc. All rights reserved.           ;
;                                                                                          ;
;  Redistribution and use in source and binary forms, with or without                      ;
;  modification, are permitted provided that the following conditions are met:             ;
;                                                                                          ;
;      * Redistributions of source code must retain the above copyright                    ;
;        notice, this list of conditions and the following disclaimer.                     ;
;      * Redistributions in binary form must reproduce the above copyright                 ;
;        notice, this list of conditions and the following disclaimer in the               ;
;        documentation and/or other materials provided with the distribution.              ;
;      * Neither the name of Fanning Software Consulting, Inc. nor the names of its        ;
;        contributors may be used to endorse or promote products derived from this         ;
;        software without specific prior written permission.                               ;
;                                                                                          ;
;  THIS SOFTWARE IS PROVIDED BY FANNING SOFTWARE CONSULTING, INC. ''AS IS'' AND ANY        ;
;  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES    ;
;  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT     ;
;  SHALL FANNING SOFTWARE CONSULTING, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,             ;
;  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED    ;
;  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;         ;
;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND             ;
;  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT              ;
;  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS           ;
;  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                            ;
;******************************************************************************************;
;
;+
; The program is used to obtain the value of a particular bit in an integer number.
;
; :Categories:
;    Utilities
;    
; :Returns:
;    The function returns the bit value (0 or 1) of a specified bit in a supplied number.
;
; :Params:
;    number: in, required, type=integer
;         The number from which the bit value is obtained. It should be a scalar integer.
;         If it is not, it is converted to a scalar integer by rounding.
;    bit: in, required, type=integer
;         The number of the bit you are interested in. A value between 0 and 63.
;         If not supplied, all 64 bit values of the number are returned. May be
;         an array of bit numbers.
;
; :Keywords:
;    silent: in, optional, type=boolean, default=0
;         If set, suppresses informational messages regarding rounding operations.
;
; :Examples:
;    Here is how to use this program::
;       IDL> !X.Style = 5
;       IDL> Print, cgBitGet(!X.Style, 2)
;            1
;
; :Author:
;    FANNING SOFTWARE CONSULTING::
;       David W. Fanning
;       1645 Sheely Drive
;       Fort Collins, CO 80526 USA
;       Phone: 970-221-0438
;       E-mail: david@idlcoyote.com
;       Coyote's Guide to IDL Programming: http://www.idlcoyote.com
;
; :History:
;     Change History::
;        Written by David W. Fanning, 14 June 2006.
;
; :Copyright:
;     Copyright (c) 2006-2014, Fanning Software Consulting, Inc.
;-
FUNCTION cgBitGet, number, bit, SILENT=silent

   ; Return to caller if something goes wrong.
   On_Error, 2

   ; Get the data type.
   theType = Size(number, /TYPE)

   ; Special pointer processing.
   IF theType EQ 10 THEN BEGIN
      theValue = *number
      theType = Size(theValue, /TYPE)
   ENDIF ELSE theValue = number

   ; Validate data type. Round any non-integer data type.
   CASE 1 OF
      theType LT 1: Message, 'VALUE must be defined.'
      theType GT 0 AND theType LT 4:
      theType GT 5 AND theType LT 10: Message, 'Wrong data type (' + Size(theValue, /TNAME) + $
         ') supplied for determining bit value.'
      theType EQ 11: Message, 'Wrong data type supplied for determining bit value.'
      theType GT 11 AND theType LT 16:
      ELSE: BEGIN
         IF ~Keyword_Set(silent) THEN Message, 'Converting value to INTEGER format...', /Informational
         theValue = Round(theValue, /L64)
         END
   ENDCASE

  ; Make sure the value is scalar.
  IF N_Elements(theValue) NE 1 THEN Message, 'Examined value must be a scalar.'
  theValue = theValue[0]

  exponent = 2ULL^UL64indgen(64)

  ; If not bit value is specifed, return all the bits in the value.
  IF N_Elements(bit) EQ 0 THEN BEGIN
      CASE 1 OF
         theType EQ 1: RETURN, Reverse(((theValue AND exponent)/exponent)[0:7])
         theType EQ 2 OR theType EQ 12: RETURN, Reverse(((theValue AND exponent)/exponent)[0:15])
         theType EQ 3 OR theType EQ 13: RETURN, Reverse(((theValue AND exponent)/exponent)[0:31])
         ELSE: RETURN, Reverse((theValue AND exponent)/exponent)
      ENDCASE
  ENDIF ELSE  bit = 0 > bit < 63 ; Confine it to correct range.

  ; Calculate the bit value and return it.
  result = ((theValue AND exponent)/exponent)[bit]
  RETURN, Convert_To_Type(result, Size(number, /TYPE))

END