DEV Community

Cover image for Bit Hacking with Go

Bit Hacking with Go

Vladimir Vivien on February 03, 2017

In the good old days of computing when memory was expensive and processing power was at premium, hacking on bits directly was the preferred (in s...
Collapse
 
markuswa_ profile image
Markus

A note on 'Bits as Configuration' - instead of assigning the values 1, 2, 4 & 8 manually you can use iota.

const (
    FlagA = (1 << iota)
    FlagB
    FlagC
    ...
)
Enter fullscreen mode Exit fullscreen mode

Makes it easier to reorder and introduce new flags.

Collapse
 
vladimirvivien profile image
Vladimir Vivien

Markus, yes thanks for pointing that out.

Collapse
 
dieter_be profile image
Dieter Plaetinck

Thanks Vladimir for this overview!
I have 2 comments:

  • there seems to be a typo in the introduction. it says '^&' which should probably be '&^'
  • the example of using 'AND NOT' to clear the 4 LSB's. do people do this? isn't it easier to just 'AND' against 0xF0 ? I tried it and it seems to provide the same outcome play.golang.org/p/UNuy05nI_X

thanks again,
Dieter

Collapse
 
vladimirvivien profile image
Vladimir Vivien

Hi Dieter,
Thanks for taking time to read and asking questions.

  • You are right, it is a typo, it should be &^ (thanks for catching that)
  • yes the AND and &^ have the same effect in some situations as you pointed out. Their usage will depend on context of what you want to achieve and which one makes the expression you are constructing easier.