ZetCode

FreeBasic Byte Keyword

last modified June 16, 2025

The FreeBasic Byte keyword represents a signed 8-bit integer data type. It can store values from -128 to 127. If you need an unsigned 8-bit integer (0 to 255), use the UByte type.

Basic Definition

In FreeBasic, Byte is a built-in data type that occupies exactly 1 byte of memory. It is signed, meaning it can represent both negative and positive values.

Byte variables are commonly used when working with binary data, file I/O, or when memory conservation is important. For unsigned 8-bit values, use UByte.

Declaring Byte Variables

This example shows how to declare and initialize Byte variables.

byte_declare.bas
Dim age As Byte = 25
Dim pixelValue As Byte = &HFF
Dim counter As Byte

Print "age: "; age
Print "pixelValue: "; pixelValue
Print "counter: "; counter

Here we declare three Byte variables. The first is initialized to 25, the second to hexadecimal FF (which is -1 as a signed Byte), and the third is uninitialized. Uninitialized Bytes default to 0 in FreeBasic.

Byte Arithmetic

Byte variables support standard arithmetic operations with automatic wrapping.

byte_arithmetic.bas
Dim a As Byte = 100
Dim b As Byte = 50
Dim result As Byte

result = a + b  ' Will wrap around
Print "100 + 50 = "; result

result = a - b
Print "100 - 50 = "; result

result = a * 2  ' Will wrap around
Print "100 * 2 = "; result

This demonstrates arithmetic with Byte values. Addition and multiplication will wrap around when exceeding the -128 to 127 range due to the 8-bit signed limit. Subtraction also wraps if the result is outside this range.

Byte Arrays

Arrays of Bytes are efficient for storing binary data or large collections of small numbers.

byte_array.bas
Dim data(4) As Byte = {65, 66, 67, 68, 69}

For i As Integer = 0 To 4
    Print "data("; i; "): "; data(i); " ('"; Chr(data(i)); "')"
Next

This creates a Byte array initialized with ASCII values. The example shows both the numeric value and its ASCII character representation. Byte arrays are memory-efficient for such data. Remember, values above 127 will wrap to negative numbers.

Byte with Binary Operations

Bytes are ideal for bitwise operations due to their 8-bit nature.

byte_bitwise.bas
Dim flags As Byte = &B10101010
Dim mask As Byte = &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 shows common bitwise operations on a Byte. The Bin function displays the binary representation. Bitwise operations are often used with Bytes for flag manipulation and low-level programming. Note that bitwise operations on signed types may yield negative results.

Byte in File I/O

Bytes are fundamental when reading and writing binary files.

byte_fileio.bas
Dim fileNum As Integer = FreeFile()
Dim buffer(9) As Byte

Open "data.bin" For Binary As #fileNum
Get #fileNum, , buffer()
Close #fileNum

For i As Integer = 0 To 9
    Print Hex(buffer(i)); " ";
Next
Print

This example reads 10 bytes from a binary file into a Byte array. Bytes are the natural data type for binary file operations, as they represent the raw data without interpretation. For values 0-255, use UByte.

Byte Type Conversion

FreeBasic automatically converts between Byte and other numeric types.

byte_conversion.bas
Dim small As Byte = 128
Dim large As Integer = 1000

' Implicit conversion (may lose data)
small = large
Print "After implicit conversion: "; small

' Explicit conversion
small = CByte(large Mod 256)
Print "After explicit conversion: "; small

This demonstrates type conversion involving Bytes. Implicit conversion from larger types truncates values to fit in 8 bits, and may result in negative numbers if the value is above 127. Explicit conversion using CByte makes the operation clearer and safer.

Byte Limits and Overflow

Understanding Byte boundaries is crucial to avoid unexpected behavior.

byte_limits.bas
Dim maxByte As Byte = 127
Dim minByte As Byte = -128

Print "Max Byte: "; maxByte
Print "Min Byte: "; minByte

maxByte += 1  ' Wraps around to -128
minByte -= 1  ' Wraps around to 127

Print "After overflow: "; maxByte
Print "After underflow: "; minByte

This shows the boundaries of the Byte type and what happens when they're exceeded. Values wrap around due to the signed 8-bit representation. Always check boundaries when working with Bytes. For 0-255, use UByte.

Best Practices

This tutorial covered the FreeBasic Byte 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.