[Implemented] Enumeration Binary

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

[Implemented] Enumeration Binary

Post by netmaestro »

Normal enumerations go up by one or you can set a step, which has to be a constant. A binary enumeration would go up by bit value, allowing for each member of the enumeration to have a bit all to itself.

Code: Select all

Enumeration Binary
  #This     ; 0
  #That     ; 1
  #TheOther ; 2
  #AndOn    ; 4
  #LikeSo   ; 8
EndEnumeration
BERESHEIT
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Enumeration Binary

Post by luis »

If you mean this -> http://www.purebasic.fr/english/viewtop ... =3&t=52244

... has been requested some times.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
oreopa
Enthusiast
Enthusiast
Posts: 281
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

Re: Enumeration Binary

Post by oreopa »

Macro is a decent workaround, but +1 tho. I often needed it.
Proud supporter of PB! * Musician * C64/6502 Freak
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Enumeration Binary

Post by User_Russian »

Justin
Addict
Addict
Posts: 832
Joined: Sat Apr 26, 2003 2:49 pm

Re: Enumeration Binary

Post by Justin »

+1
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Enumeration Binary

Post by luis »

At least seven threads now starting from 2006. :lol:
"Have you tried turning it off and on again ?"
A little PureBasic review
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Enumeration Binary

Post by Dude »

Here's my alternative. Kinda messy I admit, but a lot easier and far less typing than the other methods to specify the constants.

Code: Select all

Macro BinEnum(var)
  Enumeration (#PB_Compiler_EnumerationValue)*2
  EndEnumeration
  #var=(#PB_Compiler_EnumerationValue)/4
EndMacro

Enumeration 1
EndEnumeration

BinEnum(This)
BinEnum(That)
BinEnum(TheOther)
BinEnum(AndOn)
BinEnum(LikeSo)

Debug #This     ; 0
Debug #That     ; 1
Debug #TheOther ; 2
Debug #AndOn    ; 4
Debug #LikeSo   ; 8

Enumeration 1
EndEnumeration

BinEnum(Second)
BinEnum(Bunch)
BinEnum(Now)
BinEnum(Shown)
BinEnum(Here)

Debug #Second ; 0
Debug #Bunch  ; 1
Debug #Now    ; 2
Debug #Shown  ; 4
Debug #Here   ; 8
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Enumeration Binary

Post by Tenaja »

Thanks for sharing, Dude!
User avatar
skywalk
Addict
Addict
Posts: 3999
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Enumeration Binary

Post by skywalk »

I use this for autogenerated flags in both directions.

Code: Select all

Macro HASH
  #
EndMacro
Macro ENUM_RESET(n=0)
  Enumeration n
  EndEnumeration
EndMacro
Macro ENUM_x2(n, Name, negative=0)
  ; Create enumeration constant for later use as an index within array()'s
  ; Or as a bit flag to compare error fields.
  ; Positive (0,1,2,4,...) or Negative (0,-1,-2,-4,...) series.
  ; Enumerations support Long, Not Quad.
  CompilerIf #PB_Compiler_EnumerationValue = 0
    Enumeration #PB_Compiler_EnumerationValue
      HASH#Name#n
  CompilerElseIf #PB_Compiler_EnumerationValue = 1
    CompilerIf negative
      Enumeration #PB_Compiler_EnumerationValue Step -1
        HASH#Name#n = -#PB_Compiler_EnumerationValue
    CompilerElse
      Enumeration #PB_Compiler_EnumerationValue
        HASH#Name#n
    CompilerEndIf
  CompilerElse    ; #PB_Compiler_EnumerationValue >= 2
    CompilerIf negative
      Enumeration #PB_Compiler_EnumerationValue Step -1
        HASH#Name#n = (#PB_Compiler_EnumerationValue + 1) * 2
    CompilerElse
        Enumeration #PB_Compiler_EnumerationValue Step 1
          HASH#Name#n = (#PB_Compiler_EnumerationValue - 1) * 2
    CompilerEndIf
  CompilerEndIf
  EndEnumeration
EndMacro

ENUM_RESET(0)
Debug "-- Up --"
ENUM_x2(0, UP, 0): Debug #UP0
ENUM_x2(1, UP, 0): Debug #UP1
ENUM_x2(2, UP, 0): Debug #UP2
ENUM_x2(4, UP, 0): Debug #UP4
ENUM_RESET(0)
Debug "-- Down --"
ENUM_x2(0, DN, 1): Debug #DN0
ENUM_x2(1, DN, 1): Debug #DN1
ENUM_x2(2, DN, 1): Debug #DN2
ENUM_x2(4, DN, 1): Debug #DN4
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply