Block number conversion during bit operations

Hello I am working on problem when I use UInt32 as a bitAttay of 32 entries - (I can not use Bitarray for some reasons) problem is that sometimes (once every couple hundred iterations) when I modify the first bit it leads to Julia trying to convert it to diffrent number type and gives inexact error - How can I avoid automatic number class promotion?

@inbounds shmemblockData[x,y,1] = (shmemblockData[x,y,1] | shmemblockData[x,y,2] )

shmemblockData is UInt32 array of shape (32,32,2) x and y can by any integer between 1 and 32 information is encoded in bits - in most cases all works well but when I started benchmarking and fire the function thousands of times sometimes modyfing first bit will lead to ineaxact error which crash my program - how can I avoid it?

Thanks!!

From the symptoms you describe it sounds like your UInt32 is getting converted to a Int or Int32 somewhere along the way, gaining a negative sign, and then it’s getting upset when it tries to assign a negative number to a UInt32 location. But the line you’re showing shouldn’t cause that issue. I’d take a careful look and make sure that Ints aren’t getting accidentally introduced (perhaps via an integer literal like 2?)

If nothing else, reinterpret(UInt32,x) will convert x to a UInt32 without being angry about a negative sign. Note that if x is not 32 bits then it will throw a different error (but reinterpret(Uint32,Int32(x)) will work for any width x that can be converted). But at that point it’s definitely a sign that some promotions are happening that you aren’t intending.

2 Likes