[Matrix-SIG] bitwise combos of arrays?

David Ascher da@ski.org
Wed, 11 Aug 1999 11:03:39 -0700 (Pacific Daylight Time)


> I'm trying to combine 16 binary volumes (10 meg each) into one 16-bit
> volume. For each of the 16 volumes, the output volume would have the
> appropriate bit set. In other words, if for voxel "idx" only the 1st,
> 4th, and 9th volumes had an entry of 255 for voxel "idx" the output
> voxel at idx would be 0000000100001001.

As paul mentioned, there are bitwise operations.  You could do something
like the following, assuming that volumes is a list of 16 arrays of shape
'volumeshape' and containing zeros and ones (they will not be one-bit,
arrays, alas, as Paul also mentioned).

newvolume = zeros(volumeshape, Int16)
shift = 0
for volume in volumes:
  newvolume = bitwise_or(newvolume, volume << shift)
  shift = shift + 1

(where I most probably got the direction of the shift wrong, cf. Murphy's 
law)

The key concept for the C programmer is to use 'en-masse' opertions like
bitwise_or which operate on arrays, as opposed to thinking about things in
terms of loops.

--david