Bit flags...

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Bit flags...

emacstheviking
I really didn't think this would be so difficult! I have tried for several hours now to find an elegant solution to my problem: given a 32 bit value, create a list of atoms that represent the names of those bit positions of interest.

The problem domain: decoding a Java .class file into a meaningful compound term because I can. 
Well, I thought I could!

I initially started with the idea of using findall/3 to find all solutions etc. but really didn't get anywhere with that so I went down the more conventional recursive route

%% these define the bit positions of interest
accflag(0x0001, public).
accflag(0x0010, final).
accflag(0x0020, super).
accflag(0x0200, interface).
accflag(0x0400, abstract).

%% predicate to convert Value into a list of accflag/2 names.
access_flags(Value, Output) :-
mapflags(Value,
[0x001:public, 0x0010:final,
 0x0020:super, 0x0200:interface,
 0x0400:abstract],
Output).

mapflags has taken many incarnations!!! None of which made me happy at all so here is the question: what is the most elegant way to process an input value and create an output list of atoms for each bit that was set?

Example: access_flags(0x0601, [public,interface,abstract]).

I will continue to try to find my own way but if anybody could show me how this can be dine with findall/3 I'd be very pleased. It just feels right that there should be a solution that reads something like "for all solutions to accflag(Bit, Name) compose a list of Name-s when Value /\ Bit is true". It mushroomed because as well as checking for the end of loop condition, which I couldn't decide was best to use either an empty list of bits (or no more solutions to accflag/2) or if the input Value reaches zero because then there would be no more bits to test!

AAARGGGGH! Next month will be my 30th year in software and it gets better every day!
Once again list, thanks!
Sean
:)


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog
Reply | Threaded
Open this post in threaded view
|

Re: Bit flags...

"Micha� Bieli�ski"
Dnia 22 Października 2014, 00:54, Śr, emacstheviking napisał:
> I really didn't think this would be so difficult! I have tried for
> several hours now to find an elegant solution to my problem:
> given a 32 bit value, create a list of atoms that represent
> the names of those bit positions of interest.

Until this point it sounds easy.

> %% these define the bit positions of interest
> accflag(0x0001, public).
> accflag(0x0010, final).
> accflag(0x0020, super).
> accflag(0x0200, interface).
> accflag(0x0400, abstract).

Bit positions those are not. They are bit values.

Those are bit positions:
accflag(0, public).
accflag(4, final).
accflag(5, super).
accflag(9, interface).
accflag(10, abstract).

> %% predicate to convert Value into a list of accflag/2 names.

My way:

access_flags(Value, Output) :-
    findall(X, flag_check(Value, X), Output).

flag_check(Value, Output) :-
    accflag(X, Output),
    g_assign(z, Value),
    g_test_set_bit(z, X).

> AAARGGGGH! Next month will be my 30th year in software and it
> gets better every day!

Just out of curiosity, how do you count the time? From your first hello
world? From first year of working as programmer? Something else?

--
Michał Bieliński


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog
Reply | Threaded
Open this post in threaded view
|

Re: Bit flags...

emacstheviking
My final solution is this, which works and doesn't use global state:

accflag(0x0001, public).
accflag(0x0010, final).
accflag(0x0020, super).
accflag(0x0200, interface).
accflag(0x0400, abstract).

access_flags(Value, Output) :-
    findall(X, flag_check(Value, X), Output).

flag_check(Value, Output) :-
    accflag(B, Output),
    On is Value /\ B,
    On > 0.

Many thanks to Michał Bieliński for invaluable assistance, well, the answer pretty much, I just have to grok it now!

Excellent! :)

Sean.

On 23 October 2014 00:25, "Michał Bieliński" <[hidden email]> wrote:
Dnia 22 Października 2014, 00:54, Śr, emacstheviking napisał:
> I really didn't think this would be so difficult! I have tried for
> several hours now to find an elegant solution to my problem:
> given a 32 bit value, create a list of atoms that represent
> the names of those bit positions of interest.

Until this point it sounds easy.

> %% these define the bit positions of interest
> accflag(0x0001, public).
> accflag(0x0010, final).
> accflag(0x0020, super).
> accflag(0x0200, interface).
> accflag(0x0400, abstract).

Bit positions those are not. They are bit values.

Those are bit positions:
accflag(0, public).
accflag(4, final).
accflag(5, super).
accflag(9, interface).
accflag(10, abstract).

> %% predicate to convert Value into a list of accflag/2 names.

My way:

access_flags(Value, Output) :-
    findall(X, flag_check(Value, X), Output).

flag_check(Value, Output) :-
    accflag(X, Output),
    g_assign(z, Value),
    g_test_set_bit(z, X).

> AAARGGGGH! Next month will be my 30th year in software and it
> gets better every day!

Just out of curiosity, how do you count the time? From your first hello
world? From first year of working as programmer? Something else?

--
Michał Bieliński


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog
Reply | Threaded
Open this post in threaded view
|

Re: Bit flags...

emacstheviking
Final refinement... I know there will be a need for different sets of access flags for classes, interfaces, fields and methods etc so I have this now which I am pleased with:

access_flags(Value, Output) :-
    findall(X, flag_check(Value, accflag, X), Output).

flag_check(Value, Flag, Output) :-
    call(Flag, B, Output),
    On is Value /\ B,
    On > 0.


On 23 October 2014 14:36, emacstheviking <[hidden email]> wrote:
My final solution is this, which works and doesn't use global state:

accflag(0x0001, public).
accflag(0x0010, final).
accflag(0x0020, super).
accflag(0x0200, interface).
accflag(0x0400, abstract).

access_flags(Value, Output) :-
    findall(X, flag_check(Value, X), Output).

flag_check(Value, Output) :-
    accflag(B, Output),
    On is Value /\ B,
    On > 0.

Many thanks to Michał Bieliński for invaluable assistance, well, the answer pretty much, I just have to grok it now!

Excellent! :)

Sean.

On 23 October 2014 00:25, "Michał Bieliński" <[hidden email]> wrote:
Dnia 22 Października 2014, 00:54, Śr, emacstheviking napisał:
> I really didn't think this would be so difficult! I have tried for
> several hours now to find an elegant solution to my problem:
> given a 32 bit value, create a list of atoms that represent
> the names of those bit positions of interest.

Until this point it sounds easy.

> %% these define the bit positions of interest
> accflag(0x0001, public).
> accflag(0x0010, final).
> accflag(0x0020, super).
> accflag(0x0200, interface).
> accflag(0x0400, abstract).

Bit positions those are not. They are bit values.

Those are bit positions:
accflag(0, public).
accflag(4, final).
accflag(5, super).
accflag(9, interface).
accflag(10, abstract).

> %% predicate to convert Value into a list of accflag/2 names.

My way:

access_flags(Value, Output) :-
    findall(X, flag_check(Value, X), Output).

flag_check(Value, Output) :-
    accflag(X, Output),
    g_assign(z, Value),
    g_test_set_bit(z, X).

> AAARGGGGH! Next month will be my 30th year in software and it
> gets better every day!

Just out of curiosity, how do you count the time? From your first hello
world? From first year of working as programmer? Something else?

--
Michał Bieliński


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog



_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog