Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
Kelly Dean (krdean@lamar.colostate.edu) writes: > I need to extract the first 3 bits from a byte. > Any suggestions? If I am sure of anything, I am sure this is NOT the way to do this. :-) But you reminded me I had this program lying around here waiting for a reason to use it. One night not too long ago I couldn't sleep and for some reason (this is really *weird*) I began to wonder what the binary representation of a certain number was. Don't ask me why, I couldn't tell you. It seemed important in that strange twilight time between when you begin to lose consciousness and really fall asleep. Do you know what I mean? 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. :-) If I have the byte value 5, I would use it like this: Print, Binary(5B) It prints out: 0 0 0 0 0 1 0 1. Happy Holidays! David *********************************************************** FUNCTION BINARY, number ; This function returns the binary representation ; of a number. Numbers are converted to LONG integers ; if necessary. On_Error, 1 s = SIZE(number) type = s[s[0] + 1] IF type EQ 0 THEN Message, 'Number parameter must be defined.' IF type EQ 1 OR type EQ 2 THEN BEGIN bin = STRARR(8*type) FOR j=0,(type*8)-1 DO BEGIN powerOfTwo = 2L^j IF (LONG(number) AND powerOfTwo) EQ powerOfTwo THEN $ bin(j) = '1' ELSE bin(j) = '0' ENDFOR ENDIF ELSE BEGIN Print, 'Converting "number" to LONG...' number = LONG(number) bin = STRARR(32) FOR j=0,31 DO BEGIN powerOfTwo = 2L^j IF (LONG(number) AND powerOfTwo) EQ powerOfTwo THEN $ bin(j) = '1' ELSE bin(j) = '0' ENDFOR ENDELSE RETURN, REVERSE(bin) END ----------------------------------------------------------- David Fanning, Ph.D. Fanning Software Consulting E-Mail: davidf@dfanning.com Phone: 970-221-0438 Coyote's Guide to IDL Programming: http://www.dfanning.com/
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/
Kelly Dean wrote: > > I need to extract the first 3 bits from a byte. > > Any suggestions? > > Kelly Dean > CSU/CIRA b = 11B ; a byte of data foo = [ 2^0, 2^1, 2^2, 2^3 ] bits = ( b AND foo ) ne 0 print, bits[0] 1 print, bits[1] 1 print, bits[2] 0 print, bits[3] 1 I assume of course you have encoded information into each individual bit. If what you want is the first three bits together, it would simply be ( b AND 7 ) = 3 -- Robert M. Moss, Ph.D. - mossrm@texaco.com - FAX (713)954-6911 ------------------------------------------------------------ ----- This does not necessarily reflect the opinions of Texaco Inc.