3

I am trying to store a negative value into a Redis bitset, but the operation fails with the following error:

bit offset is not an integer or out of range

Could some please explain why storing negative numbers in Redis bitsets isn't supported?

seedhead
  • 3,655
  • 4
  • 32
  • 38

1 Answers1

2

Because nobody refers to a position in a bitset using a negative number. A bitset in an array of bit, so its index is a positive integer.

If you have a negative number (for instance coming from a hashing function), then you need to convert it to an unsigned integer first. It is straightforward do to in most language.

In the specific case of Java, to cast a signed int to an unsigned value in the bottom 32 bits of a long, you need to AND with 0xffffffffL. See the following link:

Best way to convert a signed integer to an unsigned long?

Community
  • 1
  • 1
Didier Spezia
  • 70,911
  • 12
  • 189
  • 154