How can we enumerate by the power of 2?

Just starting out? Need help? Post your questions and find answers here.
Nituvious
Addict
Addict
Posts: 1029
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

How can we enumerate by the power of 2?

Post 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
▓▓▓▓▓▒▒▒▒▒░░░░░
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

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

Post 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)
bye,
Daniel
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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)
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

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

Post by kenmo »

Ooh, that's a clever trick Demivec. I'll remember that for future big projects.
Nituvious
Addict
Addict
Posts: 1029
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

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

Post by Nituvious »

That IS very clever! Wow, thanks a bunch.
▓▓▓▓▓▒▒▒▒▒░░░░░
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

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

Post 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
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
horst
Enthusiast
Enthusiast
Posts: 197
Joined: Wed May 28, 2003 6:57 am
Location: Munich
Contact:

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

Post 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) 
Horst.
Post Reply