Go - Pipe character in Golang

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

In the package golang.org/x/sys/windows/svc there is an example that contains this code:

const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue

What does the pipe | character mean?

Answers

As others have said, it s the bitwise [inclusive] OR operator. More specifically, the operators are being used to create bit mask flags, which is a way of combining option constants based on bitwise arithmetic. For example, if you have option constants that are powers of two, like so:

const (
    red = 1 << iota    // 1 (binary: 001) (2 to the power of 0)
    green              // 2 (binary: 010) (2 to the power of 1)
    blue               // 4 (binary: 100) (2 to the power of 2)
)

Then you can combine them with the bitwise OR operator like so:

const (
    yellow = red | green          // 3 (binary: 011) (1 + 2)
    purple = red | blue           // 5 (binary: 101) (1 + 4)
    white = red | green | blue    // 7 (binary: 111) (1 + 2 + 4)
)

http://www.dylanleigh.net/notes/c-cpp-tricks.html#Using_%22Bitflags&quot; http://www.dylanleigh.net/notes/c-cpp-tricks.html#Using_%22Bitflags&quot;

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/35987779/pipe-character-in-golang

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils