Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Mapping bits to an integer

ferrad01
Beginner
1,356 Views
I have a large number (say 1000) of on/off bits of data which I want to represent as a set of integers (in this case 250 4 byte integers). I'm not sure how to do this as I don't think there is a bit datatype (1-bit). Logical*1 is 1 byte (8 bits). I'd like to do something like:

logical*1 :: lbig(1000)
integer*4 :: ibig(250)

equivalence(ibig(1), lbig(1))

but that doesn't work as the logical datatype is too big.

0 Kudos
5 Replies
jimdempseyatthecove
Honored Contributor III
1,356 Views
Look at using BIT, BIC and BIS

integer*4 :: ibig(250)

...
ibig = 0

logical function yourTest(yourArray, yourBit)
integer*4 :: yourArray(250)
integer*4 :: yourBit
yourTest = (BIT(yourArray(yourBit / 32), MOD(yourBit, 32)) .ne. 0)
end function yourTest

The yourSet and yourClear will be variations on the above

You can also use C/C++ bit array funcitons

Jim Dempsey
0 Kudos
Robert_van_Amerongen
New Contributor III
1,356 Views
If I understand it correctly, you want 1000 1/0 values, obtained from 1000 1 byte logicals, use to set the 4 bytes of 250 integers? If so, why don't use the TRANSFER statement

ibig = TRANSFER(lbig, ibig)

This works also fine in case you prefer to have the integer array being one with 1000 elements each 8 bits long!

Robert
0 Kudos
ferrad01
Beginner
1,356 Views
Thanks Jim,
This is what I need- (not sure how the TRANSFER function is supposed to work)
0 Kudos
rwg
Novice
1,356 Views
This example shows how to store and test 8000 bits. To store and test 1000 bits you need only 32 Integers so declaring ibig as integer*4::ibig(32) is sufficent.
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,356 Views
Quoting rwg
This example shows how to store and test 8000 bits. To store and test 1000 bits you need only 32 Integers so declaring ibig as integer*4::ibig(32) is sufficent.


This is correct, the code example provided was more or less a sketch of how to use a bit table.

The programmer can take this as a starting point. There are additional issues to consider for any complete implementation.

1) does the design require static allocation or dynamic allocation
2) should the bit field chunk be INTEGER*4 or INTEGER (KIND=INT_PTR_KIND())
3) Is the bit field used by a single thread or multiple threads

Issue 3, when used by multiple threads, will require atomic operations. However, by reconsidering to use 1 bit in single byte, you can then avoid an atomic operation and simply write the 0/1 into the byte.

Jim Dempsey

0 Kudos
Reply