Re: extracting bits from bytes [message #1534 is a reply to message #1532] |
Mon, 29 November 1993 18:54  |
thompson
Messages: 584 Registered: August 1991
|
Senior Member |
|
|
dean@phobos.cira.colostate.edu writes:
> I have a (graphic) file that is an 8-bit byte. Each of the 8 bits in each
> byte indicates the graphic position is on or off.
> Is there a way with IDL to determine which of the 8 bits is on (1) or
> off (0)? Like it can be done in FORTRAN or C.
The following routine does this.
Bill Thompson
============================================================ ===================
;+
; Project : SOHO - CDS
;
; Name : DEC2BIN
;
; Purpose : Convert integer decimal number to binary representation.
;
; Explanation : The binary representation of a decimal number is calculated
; and can be displayed or returned or both or neither.
;
; Use : IDL> dec2bin, decimal [, binary, /quiet]
;
; Inputs : decimal - the number to convert
;
; Opt. Inputs : None
;
; Outputs : See below
;
; Opt. Outputs: binary - the binary representation of the input.
;
; Keywords : quiet - unless given the binary number is printed to the
; terminal
;
; Calls : None
;
; Restrictions: Input must be of byte, int or long type.
;
; Side effects: None
;
; Category : Utils, Numerical
;
; Prev. Hist. : None
;
; Written : C D Pike, RAL, 7-Oct-93
;
; Modified :
;
; Version : Version 1, 7-Oct-93
;-
pro dec2bin,inp,out,quiet=quiet
;
; convert input to LONG so that arithmetic later on will work
;
in=long(inp)
;
; maximum possible output array
;
out=bytarr(32)
;
; perform the conversion
;
for i=0,31 do out(31-i)=(in and 2L^i)/2L^i
;
; trim output depending on nature of input
;
case datatype(inp) of
'BYT': begin
if not keyword_set(quiet) then print,'$(8I1,1X)',out(24:31)
out = out(24:31)
end
'INT': begin
if not keyword_set(quiet) then print,'$(2(8I1,1X))',out(16:31)
out = out(16:31)
end
'LON': begin
if not keyword_set(quiet) then print,'$(4(8I1,1X))',out
end
else: begin print,'Error: only integer types allowed.' & out = 0 & end
endcase
end
|
|
|