ZetCode

FreeBasic UByte Keyword

last modified June 16, 2025

The FreeBasic UByte keyword represents an unsigned 8-bit integer data type. It can store values from 0 to 255. UByte is useful for memory efficient storage of small positive numbers.

Basic Definition

In FreeBasic, UByte is a built-in data type that occupies exactly 1 byte of memory. Being unsigned means it can only represent non-negative values.

The UByte range is 0 to 255 (2^8-1). It's commonly used for binary data, pixel values, and when memory conservation is important. Overflow wraps around within this range.

Declaring UByte Variables

This example shows how to declare and initialize UByte variables.

ubyte_declare.bas
Dim age As UByte = 25
Dim pixelValue As UByte = 200
Dim defaultUByte As UByte

Print "age: "; age
Print "pixelValue: "; pixelValue
Print "defaultUByte: "; defaultUByte

We declare three UByte variables here. Two are initialized with values, while one uses the default value of 0. UByte variables automatically handle overflow by wrapping around.

UByte Arithmetic Operations

UByte variables support standard arithmetic operations with wrapping.

ubyte_arithmetic.bas
Dim a As UByte = 200
Dim b As UByte = 100
Dim result As UByte

result = a + b  ' 300 wraps to 44 (300-256)
Print "200 + 100 = "; result

result = a - b  ' 100
Print "200 - 100 = "; result

result = a * 2  ' 400 wraps to 144 (400-256)
Print "200 * 2 = "; result

This demonstrates UByte arithmetic with overflow handling. When results exceed 255, they wrap around to stay within the valid range. Subtraction works normally within bounds.

UByte in Arrays

UByte arrays are memory-efficient for storing small numerical data.

ubyte_array.bas
Dim grayscale(4) As UByte = {0, 64, 128, 192, 255}

For i As Integer = 0 To 4
    Print "Pixel "; i; ": "; grayscale(i)
Next

Here we create a UByte array to store grayscale pixel values. Each element uses just 1 byte of memory. This is ideal for image processing where memory usage matters.

UByte with Binary Data

UByte is perfect for working with raw binary data and file operations.

ubyte_binary.bas
Dim header(3) As UByte = {&H89, &H50, &H4E, &H47}  ' PNG signature

Print "PNG header bytes:"
For i As Integer = 0 To 3
    Print Hex(header(i)); " ";
Next
Print

This example stores PNG file signature bytes in a UByte array. Hexadecimal notation makes binary data more readable. UByte ensures exact 8-bit representation of each value.

UByte Type Conversion

FreeBasic automatically converts between UByte and other numeric types.

ubyte_conversion.bas
Dim smallNum As UByte = 200
Dim largeNum As Integer = 500

' Implicit conversion
Dim converted As UByte = largeNum  ' Wraps to 244 (500-256)
Print "Converted 500 to UByte: "; converted

' Explicit conversion
converted = CUByte(largeNum Mod 256)
Print "Explicit conversion: "; converted

This shows both implicit and explicit conversion to UByte. Large values wrap around during implicit conversion. The CUByte function provides controlled conversion.

UByte in Function Parameters

Functions can accept and return UByte values for compact data handling.

ubyte_function.bas
Function Brighten(pixel As UByte, amount As UByte) As UByte
    Dim result As Integer = pixel + amount
    If result > 255 Then result = 255
    Return result
End Function

Dim original As UByte = 200
Dim brighter As UByte = Brighten(original, 60)

Print "Original: "; original
Print "Brightened: "; brighter

This function takes UByte parameters and returns a UByte. It brightens a pixel value while clamping to prevent overflow. UByte parameters ensure input values stay within valid range.

UByte Bitwise Operations

UByte variables work well with bitwise operators for low-level manipulation.

ubyte_bitwise.bas
Dim flags As UByte = &B10101010
Dim mask As UByte = &B00001111

Print "Original: "; Bin(flags, 8)
Print "AND mask: "; Bin(flags And mask, 8)
Print "OR mask:  "; Bin(flags Or mask, 8)
Print "XOR mask: "; Bin(flags Xor mask, 8)
Print "NOT:      "; Bin(Not flags, 8)

This demonstrates bitwise operations on UByte values. The Bin function displays binary representation. UByte is ideal for flag manipulation and bitmask operations.

Best Practices

This tutorial covered the FreeBasic UByte keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.