-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_to_decimal.pl
More file actions
38 lines (28 loc) · 2.72 KB
/
binary_to_decimal.pl
File metadata and controls
38 lines (28 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
%************************************************************************************************************
% *
% Convert a Binary List Representation to Decimal Number *
% *
% The following Prolog code defines predicates `binary_to_decimal/2` and `binary_to_decimal_recursive/4` *
% to convert a binary list (LSB first) into its decimal representation. *
% *
% - `binary_to_decimal/2`: Entry point to convert binary list BinList to decimal number Dec. *
% - `binary_to_decimal_recursive/4`: Helper predicate that performs the conversion recursively. *
% *
% Example usage: *
% ?- binary_to_decimal([1, 1, 0, 1], Dec). *
% Dec = 13 *
% *
%************************************************************************************************************
% ----------------------------------------Base case: Empty list represents binary 0----------------------------------------
binary_to_decimal([], 0).
% ----------------------------------------Convert binary list BinList to decimal number Dec----------------------------------------
binary_to_decimal(BinList, Dec) :-
reverse(BinList, RevBinList), % Reverse to process LSB (Least Significant Bit) first
binary_to_decimal_recursive(RevBinList, 0, 0, Dec).
% ----------------------------------------Helper predicate to convert binary list to decimal number recursively----------------------------------------
binary_to_decimal_recursive([], _, Dec, Dec).
binary_to_decimal_recursive([Bit | Rest], Pos, Acc, Dec) :-
Bit = 0 ; Bit = 1, % Validate Bit as binary (0 or 1).
Acc1 is Acc + Bit * (2^Pos), % Compute the contribution of current bit.
Pos1 is Pos + 1, % Move to next bit position.
binary_to_decimal_recursive(Rest, Pos1, Acc1, Dec).