Search code examples
hexpascal

Condition as sum of hexadecimal numbers in old Pascal code


I've got a very old Pascal program. At the top of the unit is a set of constants looking like:

feature1 = $01; 
feature2 = $02; 
feature3 = $04;
feature4 = $08; 
feature5 = $10;

In database there is a column represents atributes and there is a sum of above features combination. I assume it might be very usefull and even today it let us avoid complicated conditional block, but i would like to read more about it and see more well explained examples of this type of code. So if you see something like that before i would be appreciated for links to some articles and excercises because i don't even know what i have to searching for in Google.


Solution

  • Here is an example using the constants in Free Pascal

    Program HexConstants;
    
    Const
        (* In Pascal a hexadecimal numeric literal is indicated by
           prefixing a numeric literal with a $ *)
        feature1 = $01;
        feature2 = $02;
        feature3 = $04;
        feature4 = $08;
        feature5 = $10;
    
    Var
        features : integer;
    
    procedure PrintFeatureDecimalValues();
    begin
        Writeln(' feature1 = ', feature1);
        Writeln(' feature2 = ', feature2);
        Writeln(' feature3 = ', feature3);
        Writeln(' feature4 = ', feature4);
        Writeln(' feature5 = ', feature5);
        Writeln('__________________________________');
    end;
    
    procedure ShowFeaturesThatAreSet(features : integer);
    begin
        if features = 0 then
            Writeln('No features are set');
        (* bitwise **and** to see if feature<n> bit is set *)
        if features and feature1 <> 0 then
            Writeln('Feature 1 is set');
        if features and feature2 <> 0 then
            Writeln('Feature 2 is set');
        if features and feature3 <> 0 then
            Writeln('Feature 3 is set');
        if features and feature4 <> 0 then
            Writeln('Feature 4 is set');
        if features and feature5 <> 0 then
            Writeln('Feature 5 is set');
        Writeln('__________________________________');
    end;
    
    Begin
        PrintFeatureDecimalValues;
        features := feature1 + feature3;
        ShowFeaturesThatAreSet(features);
        features := feature4 + feature5;
        ShowFeaturesThatAreSet(features);
        features := 0;
        ShowFeaturesThatAreSet(features);
        features := feature1 + feature2 + feature3 + feature4 + feature5;
        ShowFeaturesThatAreSet(features);
        (* adding to an already set feature *)
        features := feature2;
        features := features or feature4; (* adding feature4 *)
        ShowFeaturesThatAreSet(features);    
    End.
    

    Output:

    feature1 = 1
    feature2 = 2
    feature3 = 4
    feature4 = 8
    feature5 = 16
    __________________________________
    Feature 1 is set
    Feature 3 is set
    __________________________________
    Feature 4 is set
    Feature 5 is set
    __________________________________
    No features are set
    __________________________________
    Feature 1 is set
    Feature 2 is set
    Feature 3 is set
    Feature 4 is set
    Feature 5 is set
    __________________________________
    Feature 2 is set
    Feature 4 is set
    __________________________________