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?

Dani AI

Generated

The negative "nibbles" are caused by sign extension from arithmetic right shifts on signed integers. As discovered, shifting a byte left so its low 4 bits land in the top 4 bits of a 32‑bit signed Int32 will make the sign bit 1 whenever that 4th bit is set. Shifting back with the signed right‑shift operator reproduces the sign, so values whose 4‑bit pattern is 1000..1111 end up as -8..-1 instead of 8..15.

In other words, a 4‑bit value >= 8 is being interpreted as a 4‑bit two's‑complement signed value after the shifts. That is why only some byte values produce negative results.

Three safe fixes:

  • Mask the nibble directly (simplest and fastest):

    Dim b As Byte = bytes(n)
    Dim lowNibble As Integer = b And &H0F
  • Use unsigned arithmetic so the right shift is logical (no sign extension):

    Dim u As UInteger = CUInt(bytes(n))
    Dim lowNibble As Integer = CInt((u << 28) >> 28)
  • Extract the nibble by shifting down then masking (avoid moving bits into the sign bit at all):

    Dim highNibble As Integer = (bytes(n) >> 4) And &H0F

Be explicit about variable types. If x is an Int32 the right shift is arithmetic; using an unsigned type or masking avoids the sign‑extension problem. For reference on how VB.NET handles bitwise and shift operators, see the official docs: .

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.