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

Home » Public Forums » archive » Decomposing a bit field?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Decomposing a bit field? [message #71716] Wed, 21 July 2010 02:40 Go to next message
rjp23 is currently offline  rjp23
Messages: 97
Registered: June 2010
Member
Hi all,

I have some data that is contained within a 32-bit field.

I'm using some code from a colleague which seems to almost do what I
want but not quite.

An example of one of the data values is 15982561

The following code takes this and creates an array of 1s and 0s for
whether each bit is set or not.

test=intarr(32)
for i=0, 31 do begin test[i]=(ISHFT((15982561),-i) AND 1B)

However, for example bits 1-4 need to be combined (and contain data
from 0-15). As I don't quite understand what the code above is doing
I'm not sure how to (or if it can be) modified to not just operate on
the individual bits but on groups of bits.

What I need to extract is the information in the folowing form:

Bit 0: 0 or 1
Bits 1-4: 0-15
Bit 5: 0 or 1
Bits 6-8: 0-8
etc

Thanks in advance
Re: Decomposing a bit field? [message #71786 is a reply to message #71716] Wed, 21 July 2010 15:41 Go to previous message
Chris[6] is currently offline  Chris[6]
Messages: 84
Registered: July 2008
Member
On Jul 21, 1:25 am, Rob <rj...@le.ac.uk> wrote:
> On Jul 21, 11:29 am, Steve <f...@k.e> wrote:
>
>
>
>> Rob wrote:
>>> for i=0, 31 do begin test[i]=(ISHFT((15982561),-i) AND 1B)
>
>>> However, for example bits 1-4 need to be combined (and contain data
>>> from 0-15). As I don't quite understand what the code above is doing
>>> I'm not sure how to (or if it can be) modified to not just operate on
>>> the individual bits but on groups of bits.
>
>> ISHFT - moves bits right or left so that the bits you want are at the
>> end of the value.  AND selects the bits that you want.
>
>>> What I need to extract is the information in the folowing form:
>
>>> Bit 0:  0 or 1
>
>> bit0= ishft(15982561,0) and 1B
>
>>> Bits 1-4: 0-15
>
>> bits1to4 = ishft(15982561,-1) and 17B
>
>>> Bit 5: 0 or 1
>
>> bit5 = ishft(15982561,-5) and 1B
>
>>> Bits 6-8: 0-8
>
>> bits6to8 = ishft(15982561,-6) and 7B
>
>>> etc
>
>>> Thanks in advance
>
> Thanks everyone, that makes much more sense now.
>
> I just wanted to confirm where the 7B comes from above.
>
> I assume it's because I'm checking 3 bits so it's 1+2+4. If I had a
> group of 4 bits it's be 1+2+4+8 (15 like in Chris's example below),
> right?

right- 7 is (000000....111) in binary. So if you take "x and 7" it
will set all but the last three bits to zero. So it throws away all
the leading bits, and returns the last 3 bits as some number (0-7)

15 (0000.....1111) does the same thing, but with 4 bits instead of 3.
chris
Re: Decomposing a bit field? [message #71788 is a reply to message #71716] Wed, 21 July 2010 09:21 Go to previous message
penteado is currently offline  penteado
Messages: 866
Registered: February 2018
Senior Member
Administrator
On Jul 21, 1:04 pm, Steve <f...@k.e> wrote:
>> Thanks everyone, that makes much more sense now.
>
>> I just wanted to confirm where the 7B comes from above.
>
>> I assume it's because I'm checking 3 bits so it's 1+2+4. If I had a
>> group of 4 bits it's be 1+2+4+8 (15 like in Chris's example below),
>> right?
>
> The "B" suffix to a number tells IDL that it is to be interpreted in
> octal so indeed 17B is 8+4+2+1=15 in decimal

No, B means that it is of type byte, regardless of the base used.
Re: Decomposing a bit field? [message #71790 is a reply to message #71716] Wed, 21 July 2010 09:09 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Steve writes:

> The "B" suffix to a number tells IDL that it is to be interpreted in
> octal so indeed 17B is 8+4+2+1=15 in decimal

Well, not exactly. :-)

Octal numbers (octal numbers!?) are typically written
with a double quote in font of them:

IDL> number = "17
IDL> print, number
15

Cheers,

David



--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Decomposing a bit field? [message #71791 is a reply to message #71716] Wed, 21 July 2010 09:04 Go to previous message
Steve[5] is currently offline  Steve[5]
Messages: 10
Registered: September 2007
Junior Member
>
> Thanks everyone, that makes much more sense now.
>
> I just wanted to confirm where the 7B comes from above.
>
> I assume it's because I'm checking 3 bits so it's 1+2+4. If I had a
> group of 4 bits it's be 1+2+4+8 (15 like in Chris's example below),
> right?
>

The "B" suffix to a number tells IDL that it is to be interpreted in
octal so indeed 17B is 8+4+2+1=15 in decimal
Re: Decomposing a bit field? [message #71807 is a reply to message #71716] Wed, 21 July 2010 04:25 Go to previous message
rjp23 is currently offline  rjp23
Messages: 97
Registered: June 2010
Member
On Jul 21, 11:29 am, Steve <f...@k.e> wrote:
> Rob wrote:
>> for i=0, 31 do begin test[i]=(ISHFT((15982561),-i) AND 1B)
>
>> However, for example bits 1-4 need to be combined (and contain data
>> from 0-15). As I don't quite understand what the code above is doing
>> I'm not sure how to (or if it can be) modified to not just operate on
>> the individual bits but on groups of bits.
>
> ISHFT - moves bits right or left so that the bits you want are at the
> end of the value.  AND selects the bits that you want.
>
>
>
>> What I need to extract is the information in the folowing form:
>
>> Bit 0:  0 or 1
>
> bit0= ishft(15982561,0) and 1B
>
>> Bits 1-4: 0-15
>
> bits1to4 = ishft(15982561,-1) and 17B
>
>> Bit 5: 0 or 1
>
> bit5 = ishft(15982561,-5) and 1B
>
>> Bits 6-8: 0-8
>
> bits6to8 = ishft(15982561,-6) and 7B
>
>> etc
>
>> Thanks in advance
>
>

Thanks everyone, that makes much more sense now.

I just wanted to confirm where the 7B comes from above.

I assume it's because I'm checking 3 bits so it's 1+2+4. If I had a
group of 4 bits it's be 1+2+4+8 (15 like in Chris's example below),
right?
Re: Decomposing a bit field? [message #71809 is a reply to message #71716] Wed, 21 July 2010 03:33 Go to previous message
Chris[6] is currently offline  Chris[6]
Messages: 84
Registered: July 2008
Member
On Jul 20, 11:40 pm, Rob <rj...@le.ac.uk> wrote:
> Hi all,
>
> I have some data that is contained within a 32-bit field.
>
> I'm using some code from a colleague which seems to almost do what I
> want but not quite.
>
> An example of one of the data values is 15982561
>
> The following code takes this and creates an array of 1s and 0s for
> whether each bit is set or not.
>
> test=intarr(32)
> for i=0, 31 do begin test[i]=(ISHFT((15982561),-i) AND 1B)
>
> However, for example bits 1-4 need to be combined (and contain data
> from 0-15). As I don't quite understand what the code above is doing
> I'm not sure how to (or if it can be) modified to not just operate on
> the individual bits but on groups of bits.
>
> What I need to extract is the information in the folowing form:
>
> Bit 0:  0 or 1
> Bits 1-4: 0-15
> Bit 5: 0 or 1
> Bits 6-8: 0-8
> etc
>
> Thanks in advance

The code you attached creates an array whose ith element is 1 if the
ith bit (i.e. the 2^i component) is 1 -- confirm with an easy number
like 5 (101 in binary). So the easiest way to combine bits 1-4 would
be something like
answer = total([1, 2, 4, 8] * test[1:4])
or, more mysteriously,
answer = ishft(number, -1) and 15B

chris
Re: Decomposing a bit field? [message #71810 is a reply to message #71716] Wed, 21 July 2010 03:31 Go to previous message
greg.addr is currently offline  greg.addr
Messages: 160
Registered: May 2007
Senior Member
On 21 Jul., 11:40, Rob <rj...@le.ac.uk> wrote:
> Hi all,
>
> I have some data that is contained within a 32-bit field.
>
> I'm using some code from a colleague which seems to almost do what I
> want but not quite.
>
> An example of one of the data values is 15982561
>
> The following code takes this and creates an array of 1s and 0s for
> whether each bit is set or not.
>
> test=intarr(32)
> for i=0, 31 do begin test[i]=(ISHFT((15982561),-i) AND 1B)
>
> However, for example bits 1-4 need to be combined (and contain data
> from 0-15). As I don't quite understand what the code above is doing
> I'm not sure how to (or if it can be) modified to not just operate on
> the individual bits but on groups of bits.
>
> What I need to extract is the information in the folowing form:
>
> Bit 0:  0 or 1
> Bits 1-4: 0-15
> Bit 5: 0 or 1
> Bits 6-8: 0-8
> etc
>
> Thanks in advance

The code you have is progressively shifting the bits to the right
(i.e. dividing by 2^i) and copying the resulting least significant bit
into the test array. You can achieve what you want with something like
this:

IDL> a=15982561
IDL> print,(a and ishft(1,indgen(32))) gt 0 ;decomposition like yours
(least significant on the left)
1 0 0 0 0 1 1 1 1 1 1 1 1 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

IDL> bit=[0,1,5,6] ;start bit for each group
IDL> n_bits=[1,4,1,3] ;no. of bits in each
IDL> print,2^n_bits-1 ;max values for each group
1 15 1 7
IDL> print,2^n_bits-1 and ishft(a,-bit) ;the result
1 0 1 7

regards,
Greg
Re: Decomposing a bit field? [message #71811 is a reply to message #71716] Wed, 21 July 2010 03:29 Go to previous message
Steve[5] is currently offline  Steve[5]
Messages: 10
Registered: September 2007
Junior Member
Rob wrote:

> for i=0, 31 do begin test[i]=(ISHFT((15982561),-i) AND 1B)
>
> However, for example bits 1-4 need to be combined (and contain data
> from 0-15). As I don't quite understand what the code above is doing
> I'm not sure how to (or if it can be) modified to not just operate on
> the individual bits but on groups of bits.

ISHFT - moves bits right or left so that the bits you want are at the
end of the value. AND selects the bits that you want.
>
> What I need to extract is the information in the folowing form:
>
> Bit 0: 0 or 1

bit0= ishft(15982561,0) and 1B
> Bits 1-4: 0-15

bits1to4 = ishft(15982561,-1) and 17B

> Bit 5: 0 or 1

bit5 = ishft(15982561,-5) and 1B

> Bits 6-8: 0-8

bits6to8 = ishft(15982561,-6) and 7B

> etc
>
> Thanks in advance
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Read these data in structure
Next Topic: Re: Read these data in structure

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

Current Time: Fri Oct 10 01:32:49 PDT 2025

Total time taken to generate the page: 0.16258 seconds