SWT Style Bits – Bitwise Or and Bitwise And

Ever wondered how SWT Style bits work? It is actually very simple. The style bits are a multitud of 2

SWT.NONE = 0, // 00000000 binary
SWT.MULTI = 2, // 00000010 binary
SWT.SINGLE = 4; // 00000100 binary
SWT.READ_ONLY = 8; // 00001000 binary

Via the bitwise OR operator | you combine the bits

SWT.NONE | SWT.MULTI | SWT.READ_ONLY results in 00001010

To get later the information if a bit was set use the bitwise AND operation &.

if (SWT.WRAP==(result & SWT.WRAP)){
System.out.println(“SWT.WRAP available”);
}

Here is a small code snippets to run a little test.


package de.vogella.swt.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SWTbitwiseOr {
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		new Text(shell, SWT.NONE);
		System.out.println(SWT.NONE);
		System.out.println(SWT.MULTI);
		System.out.println(SWT.SINGLE);
		System.out.println(SWT.READ_ONLY);
		System.out.println(SWT.WRAP);
		System.out.println(SWT.SEARCH);
		System.out.println(SWT.ICON_CANCEL);
		System.out.println(SWT.ICON_SEARCH);
		System.out.println(SWT.LEFT);
		System.out.println(SWT.RIGHT);
		System.out.println(SWT.PASSWORD);
		System.out.println(SWT.CENTER);
		
		// Lets see what we have 
		
		int result = SWT.MULTI | SWT.WRAP;
		System.out.println(result);
		if (SWT.MULTI==(result & SWT.MULTI)){
			System.out.println("SWT.MULI available");	
		}
		if (SWT.WRAP==(result & SWT.WRAP)){
			System.out.println("SWT.WRAP available");	
		}
		if (SWT.SINGLE==(result & SWT.SINGLE)){
			System.out.println("SWT.SINGLE available");	
		} else {
			System.out.println("SWT.SINGLE available");	
		}
		
	}
}

For more information please see Bitwise Operators.

If you are located in Germany you may be interested in my Eclipse RCP training which I deliver together with Ralf Ebert.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Eclipse and tagged . Bookmark the permalink.

3 Responses to SWT Style Bits – Bitwise Or and Bitwise And

  1. Bibi says:

    Hello,
    You can also test with code like this:

    if ((result & SWT.MULTI) != 0) {
    ….
    }

  2. Sharon says:

    Hi,

    I thin your last “if” statement wrong? Well, not the if – but the output for the else clause. Shouldn’t it say:

    System.out.println(“SWT.SINGLE is NOT available”);

    Or is it way too early in the morning for me to be reading code? :-)

    BTW I read lots of your articles – short, sweet, easy to understand….keep ’em coming!

  3. Lars Vogel says:

    @Sharon Correct, should be “NOT available”. Thanks.

Comments are closed.