comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: bits from bytes
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: bits from bytes [message #10660] Sat, 20 December 1997 00:00 Go to previous message
Kevin Ivory is currently offline  Kevin Ivory
Messages: 71
Registered: January 1997
Member
David Fanning wrote:
> Anyway, I wanted to know. So I wrote this IDL program the
> next morning. I guess you could figure out how to use
> it to extract the first three bits from a byte, if you
> wanted to. Or, you could wait for Kevin Ivory to send you
> the *real* answer. :-)

I am a little late today. My answer is the same as the one by
Robert Moss: to extract the first three bits from a byte:
print, i_byte and 7

David, I like the idea of the function 'BINARY' to return a human
readable representation of numbers. But even a good IDL program
can become a "bit" better by eliminating the loops.
The part I included as a comment about floating point values is
what most people looking at binary representations will actually
want to have.

FUNCTION BINARY, number
;+
; PURPOSE:
; This function returns the binary representation
; of a number. Numbers are converted to LONG integers
; if necessary.
; EXAMPLE:
; Binary representation of 11B:
; IDL> print, binary(11B)
; 0 0 0 0 1 0 1 1
; If data extraction is used instead of conversion ->
; Binary representation of pi (little endian IEEE representation):
; IDL> print, format='(z9.8,5x,4(1x,8a1))', long(!pi,0), binary(!pi)
; 40490fdb 01000000 01001001 00001111 11011011
;-
On_Error, 1
s = SIZE(number)
type = s[s[0] + 1]
IF type EQ 0 THEN Message, 'Number parameter must be defined.'
bit = ['0','1']
IF type EQ 1 OR type EQ 2 THEN BEGIN
bitvalue = 2^INDGEN(8*type)
ENDIF ELSE BEGIN
Print, 'Converting "number" to LONG...'
number = LONG(number) ; data conversion
; If you want the binary representation of the floating point value,
; use extraction instead of conversion:
; number = LONG(number, 0) ; data extraction
bitvalue = 2L^LINDGEN(32)
ENDELSE

RETURN, REVERSE(bit((number AND bitvalue) EQ bitvalue))
END

Have nice and relaxing holidays,
Kevin
--
Kevin Ivory Tel: +49 5556 979 434
Max-Planck-Institut fuer Aeronomie Fax: +49 5556 979 240
Max-Planck-Str. 2 mailto:Kevin.Ivory@linmpi.mpg.de
D-37191 Katlenburg-Lindau, GERMANY http://www.gwdg.de/~kivory2/
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: Hooded Mergansers and Other Thoughts
Next Topic: getting procedures to use proper color index via LOADCT, x

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 08:16:21 PDT 2025

Total time taken to generate the page: 0.48134 seconds