Can I use base 2 (binary) numbers in the expression language?

if I use binEnc in a case, I’d like to use binary numbers instead of decimal in the case conditions to make it easier to read.

e.g.

case(binEnc(1,1,0)
   ,b000, ''
   ,b010, ''
   ,b100, ''
   ,b110, ''
   ,'Invalid'
)

Is this possible? I can’t find anything in the user manual…

I guess I could use:

case(binEnc(1,1,0)
   ,binEnc(0,0,0), ''
   ,binEnc(0,1,0), ''
   ,binEnc(1,0,0), ''
   ,binEnc(1,1,0), ''
   ,'Invalid'
)

but that’s a bit… yeah…
I know some of you are computers and can just convert 53 to 110101, but I’m not and prefer simple things like the metric system :slight_smile:

Unfortunately, I don’t think that binary literals are accepted, although hex literals are.

I’m not sure that’s more readable. It is less key strokes though. :laughing:

Also, be careful if you do go the route of binEnc(). I would look at binEnc(1,0,0) and think 4, but it’s actually computed as 1, so that could add confusion :man_shrugging:t2:

1 Like

You could use

case(fromBinary("110")
   ,fromBinary("000"), ''
   ,fromBinary("010"), ''
   ,fromBinary("100"), ''
   ,fromBinary("110"), ''
   ,'Invalid'
)
1 Like

Blegh. @nminchin , use Hex.

2 Likes

I agree with Phil here. Hex would be my choice too.

The whole point is to make it easy to see exactly what inputs you’re looking for though, hex is just a number and won’t identify bits that are on without calculation. It would be the exact same as using decimal

convert the number to bin with format for leading zeors if you wish

case(numberFormat(toBinary(3),"000")
   ,"000", '' //0
   ,"001", '' //1
   ,"010", '' //2
   ,"011", '' //3
   ,'Invalid'
)

The relationship between base 2 and base 16 for 8 bits bytes being what it is, it’s not exactly the same as decimal. Hex to binary mental conversion is relatively simple, though I agree it’s less ‘graphic’ than having the bits displayed directly.

That being said, you could maybe use hex/decimal and put the binary representation in a comment on the same line:

case(binEnc(1, 1, 0),
    0x0, "",        // 0000
    0x1, "",        // 0001
    0x2, "",        // 0010
    0x3, "",        // 0011
    "invalid"
)
1 Like

Using hex, you only need to know it one nibble at a time (see what I did there?)

3 Likes

Hex perfectly aligns with four bits at a time. A little experience, and yes, you will "see" the bits at a glance.

Hey, I did this one 2 weeks ago !

1 Like

You’re over thinkin’ it. Keep it simple and put the detail in the comments, then the next guy will know exactly what you’re on about. :+1:

I ended up going with binEnc for all, compare value and cases. Made it super easy to write and compare :slight_smile: you hex guys are all from another “base”:wink:

2 Likes