Signed and Unsigned Integers

First, the C language uses something that is unknown to VBA: unsigned numbers. VBA uses only signed numbers. An unsigned number is a positive number; that is, it does not have a sign. A signed number can be either positive or negative because of the sign. VBA does actually support one unsigned data type: Byte. For example, Figure 14-6 shows an 8-bit byte. Having a binary 1 in the most significant bit (the eighth bit) signifies that the number contained within the byte is a negative number. A 0 in the same position indicates that the number is positive.

Most significant bit

1

Figure 14-6

Sometimes you'll run across numeric parameters that require an unsigned value. The general algorithm for supplying that value is to convert the value to its next highest data type, subtract the maximum value that the data type can carry, and then convert it back to the original data type.

The following table shows how to convert values from unsigned to signed, and vice versa, for the bit widths of values supported by VBA.

Type

Convert Unsigned to Signed

Convert Signed to Unsigned

8-bit

signed = CByte(unsigned - 255)

unsigned = (CInt(signed) And 255)

16-bit

signed = CInt(unsigned - 65535)

unsigned = (CLng(signed) And 65535)

32-bit

signed = CLng(unsigned -1.79769313486232E308)

unsigned = (CDbl(signed) And 1.79769313486232E308)

+9 -5

Responses

  • seija jehkinen
    How do I create an unsigned number variable in vba?
    10 months ago

Post a comment