Page 1 of 1

How can we enumerate by the power of 2?

Posted: Sat Jun 25, 2011 5:52 pm
by Nituvious
I'm sure this a simple question but I couldn't find anything in the documents.
How can we enumerate by the power of 2 using Enumeration/EndEnumeration? Like

Code: Select all

#a = 1
#b = 2
#c = 4
#d = 8

Re: How can we enumerate by the power of 2?

Posted: Sat Jun 25, 2011 6:14 pm
by DarkDragon
You can't, but you can reformat those constants, so it shows you directly the bit which is set:

Code: Select all

#a = (1 << 0)
#b = (1 << 1)
#c = (1 << 2)
#d = (1 << 3)

Re: How can we enumerate by the power of 2?

Posted: Sat Jun 25, 2011 6:36 pm
by Demivec

Code: Select all

Macro debugBin(var)
  Debug "%" + RSet(Bin(var), 8, "0")
EndMacro

Macro nextUp
  (#PB_Compiler_EnumerationValue - 1) * 2
EndMacro

Enumeration
  #a = 1
  #b = nextUp
  #c = nextUp
  #d = nextUp
EndEnumeration

debugBin(#a)
debugBin(#b)
debugBin(#c)
debugBin(#d)

Re: How can we enumerate by the power of 2?

Posted: Sat Jun 25, 2011 6:58 pm
by kenmo
Ooh, that's a clever trick Demivec. I'll remember that for future big projects.

Re: How can we enumerate by the power of 2?

Posted: Sat Jun 25, 2011 7:09 pm
by Nituvious
That IS very clever! Wow, thanks a bunch.

Re: How can we enumerate by the power of 2?

Posted: Sun Jun 26, 2011 2:15 pm
by blueb
Nituvious wrote:I'm sure this a simple question but I couldn't find anything in the documents.
How can we enumerate by the power of 2 using Enumeration/EndEnumeration?
Sounds like you just want to double the previous constant.
So wouldn't this way be the easiest method?

Code: Select all

Enumeration 
; Used naming convention below because #E is already a constant.  
  #a1 = 1
  #b1 = #a1 * 2
  #c1 = #b1 * 2
  #d1 = #c1 * 2
  #e1 = #d1 * 2
  #f1 = #e1 * 2
  #h1 = #f1 * 2
  #i1 = #h1 * 2
  #j1 = #i1 * 2
EndEnumeration

Debug #j1
--Bob

Re: How can we enumerate by the power of 2?

Posted: Mon Jun 27, 2011 8:14 am
by horst
You can also use bit(#a) instead of #a:

Code: Select all

Macro bit(n) 
  (1 << (n))
EndMacro 

Enumeration : #a : #b : #c : #d : EndEnumeration

Debug bit(#c)
But I prefer to use the bit macro for the assignment:

Code: Select all

#a = bit(0) : #b = bit(1) : #c = bit(2) : #d = bit(3)