# [Kst] Stupid (?) question about bitwise operators in equations

Brisset, Nicolas Nicolas.Brisset at eurocopter.com
Wed Jun 13 18:56:08 CEST 2007

```One of my colleagues has files where he dumped 32-bit words which are
actually made up of single significant bits concatenated together. That
means that the resulting value interpreted as an int (or any other type)
has no meaning, all he wants to do is be able to retrieve the individual
values of his bits.
We actually already did that in the past (which is why I asked for
bitwise operators: http://bugs.kde.org/show_bug.cgi?id=88950) and there
was a discussion at that time about the fact that we were using only
doubles, which makes this tricky... I did not worry too much back then,
and even used these operators at the time of the wishlist request with
consistent results (but I may have been lucky). For the record, I was
reading INT4 fields (32 bit ints) from a CDF datasource, which are
passed to kst like :
v[i] = (double) *((Int32 *)pt); // pt is a void *
and using "[V]&(2^10)==(2^10)" as equation.

Now, I'd like to know exactly what works and what does not work with
these operators. Are there casts ? When ? Do we lose some bits in the
process ?
The chain of events as I imagine them is:
- read 32 bits from a (binary) file, consider that they are a float (or
any other type): the 32 bits lie unchanged in memory, just as they were
on the disk (hum, maybe that's only true when endianness is the same
between the machine which wrote the data and the one reading it ?)
- upcast to double for kst, store the result in V: according to the
original type given to the datasource variable that gets upcast to
double, I fear results may vary here and he sequence of bits in the end
may be different than it was on disk (and we may have a lot of extra
bits if we read a 32 bit datatype)
- use an equation on V like I did in the past: "[V]&(2^10)==(2^10)" to
retrieve the 11th bit, I expect the resulting vector to store 1.0 when
the 11th bit is set, 0.0 otherwise. There I really wonder what chances
are that we get the right result ???

I'd appreciate if one of you brilliant coders could summarize under what
circumstances the kind of equation I used will give the right results.
Thanks,

Nicolas

```

More information about the Kst mailing list