program ConvertIntToBinary;
{$mode objfpc}{$H+} // Enable modern Object Pascal features and extended syntax
{
A function that converts an integer to a binary string with leading zeros.
- 'num': The integer value to convert.
- 'bits': The target bit width (e.g., 8, 16, 32) determining the leading zeros.
}
function IntToBinary(num: LongInt; bits: Byte): string;
begin
// BinStr is a built-in FPC routine that formats the integer into a binary
// string, automatically padding with leading zeros up to the specified bit count.
Result := BinStr(num, bits);
end;
var
num: LongInt;
binStr8: string;
binStr16: string;
begin
// Initialize the data provided
num := 153;
// Convert to binary using an 8-bit width (since 153 fits within an 8-bit byte: 0 to 255)
binStr8 := IntToBinary(num, 8);
// Convert to binary using a 16-bit width to demonstrate broader padding
binStr16 := IntToBinary(num, 16);
// Display the results
Writeln('Original Integer: ', num);
Writeln('Binary (8-bit) : ', binStr8);
Writeln('Binary (16-bit) : ', binStr16);
end.
(*
run:
Original Integer: 153
Binary (8-bit) : 10011001
Binary (16-bit) : 0000000010011001
*)