What does the “|” in “int style = SWT.APPLICATION_MODAL | SWT.OK;” do (and how to Google it)?

前端 未结 7 776
囚心锁ツ
囚心锁ツ 2021-01-12 23:14

I can\'t search for | in Google. If you had found it in a software source code that you are trying to interpret, you didn\'t know what it does and you couldn\'t ask other pe

相关标签:
7条回答
  • 2021-01-12 23:23

    | is the OR operator. Those two values are probably flags that are ints with 1 bit set, and the resulting value is which style values to use.

    0 讨论(0)
  • 2021-01-12 23:25

    The pipe operator in this case means "use both SWT.APPLICATION_MODAL and SWT.OK as options/flags for my popup box." It's a very commonly used idiom with bitfield configuration identifiers, esp. in windowing systems like SWT or Win32.

    How it works

    The pipe (|) operator is the bitwise OR operator, that is, it computes an OR operation of the two binary integer values. If you check out where APPLICATION_MODAL and OK are defined, you'll find they are something like this:

    ...
    SWT.OK = 1,                  // 00000001 in binary
    SWT.ABORT_RETRY_IGNORE = 2,  // 00000010 in binary
    SWT.OK_CANCEL = 4;           // 00000100 in binary
    ...
    SWT.APPLICATION_MODAL = 32;  // 00100000 in binary
    ... (and so on...)
    

    When you bitwise OR two (or more) of these numbers together, individual bits will be set for each of the options:

    int style = SWT.OK | SWT.APPLICATION_MODAL = 00000001 | 00100000 = 00100001
    

    The windowing toolkit that goes to interpret style will be able to tell exactly what you wanted (a popup box that is Modal and has an OK button) by doing a bitwise AND like this:

    ...
    if(style & SWT.OK)
    {
        // we want an OK box
    }
    if(style & SWT.ABORT_RETRY_IGNORE)
    {
        // we want an Abort/Retry/Ignore box
    }
    if(style & SWT.OK_CANCEL)
    {
        // we want an OK/Cancel box
    }
    ...
    if(style & SWT.APPLICATION_MODAL)
    {
        // We want a modal box
    }
    ...
    

    Kinda clever, in my humble opinion. It allows you to select/represent multiple configuration options in a single variable. The trick is in the integer definitions of the options, and ensuring that they are only powers of 2.

    0 讨论(0)
  • 2021-01-12 23:27

    Unfortunately, Google is lousy at symbols. Instead, I'd start by looking at the Wikipedia page, and search on there for "|", which is at:

    • bitwise logic (~, &, |, ^)

    and that page has a pretty good overview of what's what and how to use it.

    0 讨论(0)
  • 2021-01-12 23:36

    Use a search engine that can handle it: A | B

    0 讨论(0)
  • 2021-01-12 23:39

    It is a bitwise OR, it provides a means for you to pass a number of flags to the SWT component as a single int, rather than having to overload the type with loads of setter methods.

    The two properties you list indicate you have a dialog or other window that should be displayed modally over the parent shell, and use the ok button. You could combine it with SWT.CANCEL to display an OK and Cancel button for instance

    0 讨论(0)
  • 2021-01-12 23:46

    Bitwise OR.

    With most things, there's a bit of a learning curve. If I was learning a new language, I would assume that | was an operator, and would search for 'Java operators'.

    0 讨论(0)
提交回复
热议问题