This Christmas day, I am plagued by this negative nibble mystery. I have some bytes in a byte array that I want to split into nibbles

x = bytes(n)
x = x << 28'cause the left 4 bits to overflow so we remain with only the right 4 bits
x = x >> 28

This code works fine, except for some integers including those between 100 and 110. I have, however, solved this problem by ANDing 15 to whatever value the function returns.

I have tried thinking about how the negative nibbles come about, but I haven't managed to find out how. This is where you come in. May someone please explain how it happens?

Are 100 and 110 mentioned in your post denary or binary values?

Denary. The values that bring negative nibbles are 101, 102 ,103, 104...110.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.