page 1 of 1
Comments: 16 | Views: 768

I have a situation where I need to create many enum values that have very specific, non sequential binary patterns. You have decimal, hex and octal notation, but why no plain old binary notation?

 

I can do this:

 

public enum MyCodes

{

  x = 0x80FA,

  ...

}

 

But I can't do something like:

 

public enum MyCodes

{

  x = 0b110110110,

  ...

}

 

It seems like an oversight to me. Yes this is not needed often, and I can manually convert to hex for each of my values, but it seems you'd need binary way before you would ever need octal, yet there is no binary notation.

 

Am I missing something?

Probably because hex is about a million times easier to type in correctly and converting a binary number into hex is trivially done in your head.

W3bbo
W3bbo
The Master of Baiters

If you're using C/C++ just define a preprocessor macro; if you're using C# then write a simple preprocessor and run it before compilation in your build cycle (it might conflict with the IDE's syntax checker though).

Matthew van Eerde
Matthew van Eerde
AKA Maurits

I'd like a hex float literal while we're at it.

figuerres
figuerres
???

well honestly our good friend may be a bit fast on that....  not super hard but can be a tad tricky to make it bullet proof.

 

a "Pre-Processor" in terms of compiling is a program that takes an input and makes an output that can then be passed on to another program.

you would read the .cs file and replace all of the 0b010 text with hex or int values that the default c# compiler can use.

a few details would be scanning the text so that you do not modify comments or strings...

you would have to lex/parse enought to be sure the text you would replace was the right text.

 

in classic old time C  there used to be 2 binaries one just did macros and the other one took the first steps result and generated the assembly source, the old time build system used to then run an assembler to convert to an object file and then you ran a linker and then you had a working binary program.

 

oh and the old preproc stage also did the old #include lines merging them into the next stage.

 

*.c ==> *.asm ==> a.out ==>  program

Are you trying to do somthing like:

 

public enum SECURITY_MASKS {

            NO_ACCESS = 0,

            STANDARD_USER = 1 << 0,

            FINANCE_USER = 1 << 1,

            ADMIN_USER = 1 << 2

}

 

 

I have similiar logic in several of my projects.  Works like a champ.          

W3bbo
W3bbo
The Master of Baiters

Ugh no, that requires too much thinking!

It doesn't take much to mentally "see" the binary patterns while looking at hex and the natural 4-bit groupings make it easy to be sure you're looking at the right bits. Much easier than looking at long binary strings.

 

Or maybe I just spent too many years writing assembler. Tongue Out

While I admit I can see HEX in my sleep, you waste a bunch of bits that way.  Using the 1 << 0, 1<<1 uses all bits.  Your solution keeps things in four bit nibbles.  My solution uses each and every bit to form unique masks.

 

My method allows you to do things like:

 

if ((UserAccess & (SECURITY_MASKS.STANDARD_USER | SECURITY_MASKS.FINANCE_USER)) != 0) {

  // user has either STANDARD_USER or FINANCE_USER "roles"

}

 

 

I don't see how, there is absolutely no difference in the underlying value (1, 2, 4, 8, 16, 32 etc). I'd be inclined to use your approach when defining constants because I find it slightly more readable, but it ultimately makes no difference.

 

My point is, given a long bit string with multiple bits set, it's a about a gazillion times easier to read/write hex and yet still be able to "see" the bit pattern. And I suspect that is why there isn't a simple way of defining constants in binary.

ManipUni
ManipUni
Proving QQ for 5 years!

Binary is too small to write accurately to memory. So if you had a constant bin "1" what the heck would the compiler do? Write "0001" is that accurate? If that is accurate then having a binary constant is pointless, if it is inaccurate then having one is broken.

 

So you have either useless or broken. Your call on which one best describes why compilers lack constants Smiley

 

 

case binarystring(n: int)

when "001110011",...

 

or whatever the syntax

?

Huh? What are you talking about?

 

That's like saying you can't have an Integer constant because 6 is different to 06 or 0000006. Leading zeroes are as meaningless in binary* as they are in decimal or hex. Binary constants don't exist because they're probably not worth the effort, nobody would use them on a day to day basis and in pretty much every case you really need to know hex is a better, more readable, choice.

 

*And before anyone mentions sign bits, that is only relevant when you give a bit pattern context. At which point it has a fixed size and thus there is no ambiguity.

page 1 of 1
Comments: 16 | Views: 768
Microsoft Communities