Computer Science Reference Operators

Embed Size (px)

Citation preview

  • 8/9/2019 Computer Science Reference Operators

    1/53

    3.Operators

    3. 1.Operators( 5 ) 3. 6.Relational Operators( 2 )

    3. 2.Assignment Operators( 1 ) 3. 7.Logical Operators( 10 )

    3. 3.Increment Decrement Operators( 5 ) 3. 8.Ternary Operator( 1 )

    3. 4.Arithmetic Operators( 6 ) 3. 9.Comma Operator( 1 )3. 5.Bitwise Operators( 23 ) 3. 10.instanceof( 4 )

    3. 1. Operators

    3. 1. 1. Six categories of operators

    3. 1. 2. Operator Precedence

    3. 1. 3. The op= Operators

    3. 1. 4.The ternary operator (The Conditional Operator): result =value>conditionValue ? result1 : result2

    3. 1. 5.Tests all the operators on all the primitive data types to showwhich ones are accepted by the Java compiler

    3. 1. 1. Six categories of operators

    In Java, there are six categories of operators.

    1. Unary operators2. Arithmetic operators3. Relational and conditional operators4. Shift and logical operators5. Assignment operators6. Other operators

    Precedence Operator Description Association1 ++,-- Postincrement, Postdecrement R -> L

    2 ++,-- Preincrement, Predecrement R -> L+,- Unary plus, unary minus R -> L~ Bitwise compliment R -> L! Boolean NOT R -> L

    3 new Create object R -> L(type) Type cast R -> L

    4 *,/,% Multiplication, division, remainder L -> R5 +,- Addition, subtraction L -> R

    + String concatenation L -> R

    http://www.java2s.com/Tutorial/Java/0060__Operators/Catalog0060__Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Catalog0060__Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Catalog0060__Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0200__Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0200__Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0200__Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0320__Relational-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0320__Relational-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0320__Relational-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0240__Assignment-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0240__Assignment-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0240__Assignment-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0340__Logical-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0340__Logical-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0340__Logical-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0260__Increment-Decrement-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0260__Increment-Decrement-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0260__Increment-Decrement-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0360__Ternary-Operator.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0360__Ternary-Operator.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0360__Ternary-Operator.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0280__Arithmetic-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0280__Arithmetic-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0280__Arithmetic-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0380__Comma-Operator.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0380__Comma-Operator.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0380__Comma-Operator.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0300__Bitwise-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0300__Bitwise-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0300__Bitwise-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0400__instanceof.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0400__instanceof.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0400__instanceof.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Sixcategoriesofoperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Sixcategoriesofoperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/OperatorPrecedence.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/OperatorPrecedence.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheopOperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheternaryoperatorTheConditionalOperatorresultvalueconditionValueresult1result2.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheternaryoperatorTheConditionalOperatorresultvalueconditionValueresult1result2.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheternaryoperatorTheConditionalOperatorresultvalueconditionValueresult1result2.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TestsalltheoperatorsonalltheprimitivedatatypestoshowwhichonesareacceptedbytheJavacompiler.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TestsalltheoperatorsonalltheprimitivedatatypestoshowwhichonesareacceptedbytheJavacompiler.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TestsalltheoperatorsonalltheprimitivedatatypestoshowwhichonesareacceptedbytheJavacompiler.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TestsalltheoperatorsonalltheprimitivedatatypestoshowwhichonesareacceptedbytheJavacompiler.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TestsalltheoperatorsonalltheprimitivedatatypestoshowwhichonesareacceptedbytheJavacompiler.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheternaryoperatorTheConditionalOperatorresultvalueconditionValueresult1result2.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheternaryoperatorTheConditionalOperatorresultvalueconditionValueresult1result2.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheopOperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/OperatorPrecedence.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Sixcategoriesofoperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0400__instanceof.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0300__Bitwise-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0380__Comma-Operator.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0280__Arithmetic-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0360__Ternary-Operator.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0260__Increment-Decrement-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0340__Logical-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0240__Assignment-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0320__Relational-Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/0200__Operators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Catalog0060__Operators.htm
  • 8/9/2019 Computer Science Reference Operators

    2/53

    6 , >>> Left shift, right shift, unsigned right shift L -> R7 = L -> R

    instanceof Type comparison L -> R8 ==, != Value equality and inequality L -> R

    ==, != Reference equality and inequality L -> R9 & Boolean AND L -> R

    & Bitwise AND L -> R10 ^ Boolean XOR L -> R^ Bitwise XOR L -> R

    11 | Boolean OR L -> R| Bitwise OR L -> R

    12 && Conditional AND L -> R13 || Conditional OR L -> R14 ?: Conditional Ternary Operator L -> R15 =,+=,-=, Assignment Operators R -> L

    *=,/ =,%=,&=,^=, |=, =,>>>=

    3. 1. 2. Operator Precedence

    Operators with a higher precedence are executed before those of a lower precedence.

    Operators on the same line have the same precedence:

    Operator Precedence Group AssociativityOperator Precedence

    (), [], postfix ++, postfix -- left Highest

    unary +, unary -, prefix ++, prefix --, ~, ! right

    (type), new left

    *, /, % left+, - left

    , >>> left

    < ,, >=, instanceof

    ==, !=

    & left

    ^ left

    | left

    && left

    || left

    ?: left

    =, +=, -=, *=, /=, %=, =, >>>=, &=, |=, ^= right lowest

    3. 1. 3. The op= Operators

  • 8/9/2019 Computer Science Reference Operators

    3/53

    count += 5 has the same effect as the statement: count = count + 5;

    public class MainClass {

    public static voidmain(String[] arg) {int count = 1;

    count += 5;System.out.println(count);

    count = count + 5;System.out.println(count);

    }

    }

    611

    The complete set of op= operators:

    1. +=2. -=3. *=4. /=5. %=6. =8. >>>=9. &=

    10. |=11. =

    3. 1. 4. The ternary operator (The Conditional Operator):result = value>conditionValue ? result1 : result2

    if(value > conditionValue){result = result1;

    }else{result = result2;

    }

    logical_expression ? expression1 : expression2

    public class MainClass {public static voidmain(String[] args) {

    int v = 1;System.out.println(v == 1 ? "A" : "B");

  • 8/9/2019 Computer Science Reference Operators

    4/53

    v++;System.out.println(v == 1 ? "A" : "B");

    }}

    AB

    3. 1. 5. Tests all the operators on all the primitive data typeones are accepted by the Java compiler

    public class MainClass {public static voidmain(String[] a){

    boolTest(true, false);charTest('x', 'y');byteTest((byte)0, (byte)1);shortTest((short)0, (short)1);intTest(1, 2);longTest(11L, 22L);floatTest(1.1F, 2.2F);doubleTest(1.1, 2.2);

    }

    // To accept the results of a boolean test:static voidf(boolean b) {

    System.out.println("f:"+b);}static voidboolTest(boolean x,boolean y) {

    // Arithmetic operators://! x = x * y;//! x = x / y;//! x = x % y;//! x = x + y;//! x = x - y;//! x++;//! x--;//! x = +y;//! x = -y;// Relational and logical://! f(x > y);//! f(x >= y);//! f(x < y);//! f(x

  • 8/9/2019 Computer Science Reference Operators

    5/53

    x = x | y;x = x ^ y;//! x = x > 1;//! x = x >>> 1;// Compound assignment:

    //! x += y;//! x -= y;//! x *= y;//! x /= y;//! x %= y;//! x = 1;//! x >>>= 1;x &= y;x ^= y;x |= y;// Casting://! char c = (char)x;//! byte B = (byte)x;//! short s = (short)x;//! int i = (int)x;//! long l = (long)x;//! float f = (float)x;//! double d = (double)x;

    }static voidcharTest(char x, char y) {

    // Arithmetic operators:x = (char)(x * y);x = (char)(x / y);x = (char)(x % y);x = (char)(x + y);x = (char)(x - y);

    x++;x--;x = (char)+y;x = (char)-y;// Relational and logical:f(x > y);f(x >= y);f(x < y);f(x 1);x = (char)(x >>> 1);// Compound assignment:

  • 8/9/2019 Computer Science Reference Operators

    6/53

    x += y;x -= y;x *= y;x /= y;x %= y;x = 1;x >>>= 1;x &= y;x ^= y;x |= y;// Casting://! boolean b = (boolean)x;byte B = (byte)x;short s = (short)x;int i = (int)x;long l = (long)x;float f = (float)x;double d = (double)x;

    }static voidbyteTest(byte x,byte y) {

    // Arithmetic operators:x = (byte)(x* y);x = (byte)(x / y);x = (byte)(x % y);x = (byte)(x + y);x = (byte)(x - y);x++;x--;x = (byte)+ y;x = (byte)- y;// Relational and logical:f(x > y);

    f(x >= y);f(x < y);f(x 1);x = (byte)(x >>> 1);// Compound assignment:x += y;x -= y;x *= y;x /= y;x %= y;x

  • 8/9/2019 Computer Science Reference Operators

    7/53

    x >>= 1;x >>>= 1;x &= y;x ^= y;x |= y;// Casting:

    //! boolean b = (boolean)x;char c = (char)x;short s = (short)x;int i = (int)x;long l = (long)x;float f = (float)x;double d = (double)x;

    }static voidshortTest(short x, short y) {

    // Arithmetic operators:x = (short)(x * y);x = (short)(x / y);x = (short)(x % y);x = (short)(x + y);x = (short)(x - y);x++;x--;x = (short)+y;x = (short)-y;// Relational and logical:f(x > y);f(x >= y);f(x < y);f(x 1);x = (short)(x >>> 1);// Compound assignment:x += y;x -= y;x *= y;x /= y;x %= y;x = 1;x >>>= 1;x &= y;x ^= y;x |= y;// Casting:

  • 8/9/2019 Computer Science Reference Operators

    8/53

    //! boolean b = (boolean)x;char c = (char)x;byte B = (byte)x;int i = (int)x;long l = (long)x;float f = (float)x;

    double d = (double)x;}static voidintTest(int x, int y) {

    // Arithmetic operators:x = x * y;x = x / y;x = x % y;x = x + y;x = x - y;x++;x--;x = +y;x = -y;// Relational and logical:f(x > y);f(x >= y);f(x < y);f(x 1;x = x >>> 1;// Compound assignment:x += y;x -= y;x *= y;x /= y;x %= y;x = 1;x >>>= 1;x &= y;x ^= y;x |= y;// Casting://! boolean b = (boolean)x;char c = (char)x;byte B = (byte)x;short s = (short)x;long l = (long)x;float f = (float)x;

  • 8/9/2019 Computer Science Reference Operators

    9/53

    double d = (double)x;}static voidlongTest(long x, long y) {

    // Arithmetic operators:x = x * y;x = x / y;

    x = x % y;x = x + y;x = x - y;x++;x--;x = +y;x = -y;// Relational and logical:f(x > y);f(x >= y);f(x < y);f(x 1;x = x >>> 1;// Compound assignment:x += y;

    x -= y;x *= y;x /= y;x %= y;x = 1;x >>>= 1;x &= y;x ^= y;x |= y;// Casting://! boolean b = (boolean)x;char c = (char)x;byte B = (byte)x;short s = (short)x;int i = (int)x;float f = (float)x;double d = (double)x;

    }static voidfloatTest(float x, float y) {

    // Arithmetic operators:x = x * y;x = x / y;

  • 8/9/2019 Computer Science Reference Operators

    10/53

    x = x % y;x = x + y;x = x - y;x++;x--;x = +y;

    x = -y;// Relational and logical:f(x > y);f(x >= y);f(x < y);f(x 1;//! x = x >>> 1;// Compound assignment:x += y;x -= y;x *= y;x /= y;x %= y;//! x = 1;

    //! x >>>= 1;//! x &= y;//! x ^= y;//! x |= y;// Casting://! boolean b = (boolean)x;char c = (char)x;byte B = (byte)x;short s = (short)x;int i = (int)x;long l = (long)x;double d = (double)x;

    }static voiddoubleTest(double x, double y) {

    // Arithmetic operators:x = x * y;x = x / y;x = x % y;x = x + y;x = x - y;x++;x--;x = +y;

  • 8/9/2019 Computer Science Reference Operators

    11/53

    x = -y;// Relational and logical:f(x > y);f(x >= y);f(x < y);f(x 1;//! x = x >>> 1;// Compound assignment:x += y;x -= y;x *= y;x /= y;x %= y;//! x = 1;//! x >>>= 1;//! x &= y;//! x ^= y;//! x |= y;// Casting://! boolean b = (boolean)x;

    char c = (char)x;byte B = (byte)x;short s = (short)x;int i = (int)x;long l = (long)x;float f = (float)x;

    }}

    3. 2. Assignment Operators

    3. 2. 1. Several assignment operators

    3. 2. 1. Several assignment operators

    public class MainClass {public static voidmain(String args[]) {

    int a = 1;int b = 2;

    http://www.java2s.com/Tutorial/Java/0060__Operators/Severalassignmentoperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Severalassignmentoperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Severalassignmentoperators.htm
  • 8/9/2019 Computer Science Reference Operators

    12/53

    int c = 3;

    a += 5;b *= 4;c += a * b;c %= 6;

    System.out.println("a = " + a);System.out.println("b = " + b);System.out.println("c = " + c);

    }}

    a = 6b = 8c = 3

    3. 3. Increment Decrement Operators

    3. 3. 1. Increment and Decrement: Demonstrate ++.

    3. 3. 2. The increment and decrement operators

    3. 3. 3. Using the increment and decrement operators in an expression

    3. 3. 4. The prefix form and the postfix form

    3. 3. 5. Using ++ and -- with floating-point variables

    3. 3. 1. Increment and Decrement: Demonstrate ++.

    public class MainClass {public static voidmain(String args[]) {

    int a = 1;int b = 2;int c;int d;c = ++b;d = a++;c++;System.out.println("a = " + a);System.out.println("b = " + b);System.out.println("c = " + c);System.out.println("d = " + d);

    }}

    a = 2

    http://www.java2s.com/Tutorial/Java/0060__Operators/IncrementandDecrementDemonstrate.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/IncrementandDecrementDemonstrate.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Theincrementanddecrementoperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Theincrementanddecrementoperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Usingtheincrementanddecrementoperatorsinanexpression.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Usingtheincrementanddecrementoperatorsinanexpression.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Theprefixformandthepostfixform.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Theprefixformandthepostfixform.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Usingandwithfloatingpointvariables.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Usingandwithfloatingpointvariables.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Usingandwithfloatingpointvariables.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Theprefixformandthepostfixform.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Usingtheincrementanddecrementoperatorsinanexpression.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Theincrementanddecrementoperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/IncrementandDecrementDemonstrate.htm
  • 8/9/2019 Computer Science Reference Operators

    13/53

    b = 3c = 4d = 1

    3. 3. 2. The increment and decrement operators

    1. add an integer variable by one.2. increment operator: two successive plus signs, ++.3. decrement operator: --.

    public class MainClass {

    public static voidmain(String[] argv) {int count = 10;++count; // Add 1 to count--count; // Subtract 1 from count

    System.out.println(count);}

    }

    10

    3. 3. 3. Using the increment and decrement operators in a

    public class MainClass {public static voidmain(String[] args) {

    int numA = 5;int numB = 10;int numC = 0;

    numC = ++numA + numB;

    System.out.println(numA);System.out.println(numC);

    }}

    616

    3. 3. 4. The prefix form and the postfix form

  • 8/9/2019 Computer Science Reference Operators

    14/53

    public class MainClass {public static voidmain(String[] args) {

    int numA = 5;int numB = 10;int numC = 0;

    numC = ++numA + numB;

    System.out.println(numA);System.out.println(numC);

    }}

    616

    public class MainClass {public static voidmain(String[] args) {

    int numA = 5;

    int numB = 10;int numC = 0;

    numC = --numA + numB--;System.out.println(numA);System.out.println(numC);

    }}

    414

    3. 3. 5. Using ++ and -- with floating-point variables

    public class MainClass{

    public static voidmain(String[] arg){double a = 12.12;

    System.out.println( a-- );System.out.println( a++ );System.out.println( --a );System.out.println( ++a );

    }

    }

    12.1211.1211.12

  • 8/9/2019 Computer Science Reference Operators

    15/53

    12.12

    3. 4. Arithmetic Operators

    3. 4. 1. Arithmetic Calculations

    3. 4. 2. Arithmetic Operators

    3. 4. 3. The Basic Arithmetic Operators

    3. 4. 4. Demonstrates the mathematical operators

    3. 4. 5. Modulus operator %: obtain the remainder after a division

    3. 4. 6. Applying the modulus operator, %, to floating-point values 3. 4. 1. Arithmetic Calculations

    An assignment statement has three elements:

    1. the variable to store the result,2. the assignment operator: =,3. an arithmetic expression

    The statement is terminated by a semicolon.

    public class MainClass{public static voidmain(String[] argv){

    int a = 1;int b = 2;int c = 0;

    c = a + b;

    System.out.println(c);}

    }

    3

    3. 4. 2. Arithmetic Operators

    OperatorResult

    + Addition

    http://www.java2s.com/Tutorial/Java/0060__Operators/ArithmeticCalculations.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/ArithmeticOperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheBasicArithmeticOperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheBasicArithmeticOperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Demonstratesthemathematicaloperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Demonstratesthemathematicaloperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Modulusoperatorobtaintheremainderafteradivision.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Modulusoperatorobtaintheremainderafteradivision.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Applyingthemodulusoperatortofloatingpointvalues.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Applyingthemodulusoperatortofloatingpointvalues.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Modulusoperatorobtaintheremainderafteradivision.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Demonstratesthemathematicaloperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheBasicArithmeticOperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/ArithmeticOperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/ArithmeticCalculations.htm
  • 8/9/2019 Computer Science Reference Operators

    16/53

    - Subtraction (also unary minus)

    * Multiplication

    / Division

    % Modulus

    ++ Increment+= Addition assignment

    -= Subtraction assignment

    *= Multiplication assignment

    /= Division assignment

    %= Modulus assignment

    -- Decrement

    3. 4. 3. The Basic Arithmetic Operators

    public class MainClass {public static voidmain(String args[]) {

    // arithmetic using integersSystem.out.println("Integer Arithmetic");int a = 1 + 1;int b = a * 3;int c = b / 4;int d = c - a;int e = -d;System.out.println("a = " + a);System.out.println("b = " + b);System.out.println("c = " + c);System.out.println("d = " + d);System.out.println("e = " + e);

    // arithmetic using doublesSystem.out.println("\nFloating Point Arithmetic");double da = 1 + 1;double db = da * 3;double dc = db / 4;double dd = dc - a;double de = -dd;System.out.println("da = " + da);System.out.println("db = " + db);System.out.println("dc = " + dc);

    System.out.println("dd = " + dd);System.out.println("de = " + de);

    }}

    Integer Arithmetica = 2b = 6

  • 8/9/2019 Computer Science Reference Operators

    17/53

    c = 1d = -1e = 1

    Floating Point Arithmetic

    da = 2.0db = 6.0dc = 1.5dd = -0.5de = 0.5

    3. 4. 4. Demonstrates the mathematical operators

    import java.util.Random;

    public class MainClass {static voidprintInt(String s, int i) {

    System.out.println(s + " = " + i);}

    static voidprintFloat(String s, float f) {System.out.println(s + " = " + f);

    }

    public static voidmain(String[] args) {Random rand = new Random();int i, j, k;j = rand.nextInt(100) + 1;

    k = rand.nextInt(100) + 1;printInt("j", j);printInt("k", k);i = j + k;printInt("j + k", i);i = j - k;printInt("j - k", i);i = k / j;printInt("k / j", i);i = k * j;printInt("k * j", i);i = k % j;printInt("k % j", i);

    j %= k;printInt("j %= k", j);// Floating-point number tests:float u, v, w; // applies to doubles, toov = rand.nextFloat();w = rand.nextFloat();printFloat("v", v);printFloat("w", w);u = v + w;

  • 8/9/2019 Computer Science Reference Operators

    18/53

    printFloat("v + w", u);u = v - w;printFloat("v - w", u);u = v * w;printFloat("v * w", u);u = v / w;

    printFloat("v / w", u);// the following also works for// char, byte, short, int, long,// and double:u += v;printFloat("u += v", u);u -= v;printFloat("u -= v", u);u *= v;printFloat("u *= v", u);u /= v;printFloat("u /= v", u);

    }}

    j = 31k = 84j + k = 115j - k = -53k / j = 2k * j = 2604k % j = 22j %= k = 31v = 0.79780066w = 0.9309381v + w = 1.7287388v - w = -0.13313746v * w = 0.7427031v / w = 0.8569857u += v = 1.6547863u -= v = 0.8569857u *= v = 0.6837037u /= v = 0.8569856

    3. 4. 5. Modulus operator %: obtain the remainder after a d

    public class MainClass {

    public static voidmain(String[] argv) {int a = 3 % 2;int b = 11 % 3;int c = 7 % -3;

  • 8/9/2019 Computer Science Reference Operators

    19/53

    System.out.println(a);System.out.println(b);System.out.println(c);

    }

    }

    121

    3. 4. 6. Applying the modulus operator, %, to floating-point

    public class MainClass{

    public static voidmain(String[] arg){double a = 2.4;double b = 0.2;

    System.out.println( a % b );}

    }

    0.1999999999999998

    3. 5. Bitwise Operators

    3. 5. 1.The Bitwise Operators can be applied to the integer types, long, int,

    short, char, and byte.

    3. 5. 2. The Bitwise Logical Operators

    3. 5. 3. Bitwise AND (&)

    3. 5. 4. Bitwise OR (|)

    3. 5. 5. Bitwise XOR (^)

    3. 5. 6. Left shift (

  • 8/9/2019 Computer Science Reference Operators

    20/53

    3. 5. 8. Demonstrate the bitwise logical operators

    3. 5. 9. All bitwise operators in action

    3. 5. 10. Bitwise Operator Assignments

    3. 5. 11. The Left Shift

    3. 5. 12. Left shifting as a quick way to multiply by 2

    3. 5. 13. The Right Shift

    3. 5. 14. The Unsigned Right Shift

    3. 5. 15. Signed shift to the right

    3. 5. 16. Unsigned shifting a byte value.

    3. 5. 17. Convert a number to negative and back

    3. 5. 18. Performing Bitwise Operations on a Bit Vector

    3. 5. 19. Converting Between a BitSet and a Byte Array

    3. 5. 20. Returns a byte array of at least length 1

    3. 5. 21. Use bitwise operator to create hash code

    3. 5. 22. Operations on bit-mapped fields.

    3. 5. 23. Represents a collection of 64 boolean (on/off) flags.

    3. 5. 1. The Bitwise Operators can be applied to the integer type

    int, short, char, and byte.

    OperatorResult

    ~ Bitwise unary NOT

    & Bitwise AND

    | Bitwise OR

    http://www.java2s.com/Tutorial/Java/0060__Operators/Demonstratethebitwiselogicaloperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Demonstratethebitwiselogicaloperators.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Allbitwiseoperatorsinaction.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Allbitwiseoperatorsinaction.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/BitwiseOperatorAssignments.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/BitwiseOperatorAssignments.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheLeftShift.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Leftshiftingasaquickwaytomultiplyby2.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Leftshiftingasaquickwaytomultiplyby2.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheRightShift.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheUnsignedRightShift.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheUnsignedRightShift.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Signedshifttotheright.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Unsignedshiftingabytevalue.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Unsignedshiftingabytevalue.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Convertanumbertonegativeandback.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Convertanumbertonegativeandback.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/PerformingBitwiseOperationsonaBitVector.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/PerformingBitwiseOperationsonaBitVector.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/ConvertingBetweenaBitSetandaByteArray.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Returnsabytearrayofatleastlength1.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Returnsabytearrayofatleastlength1.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Usebitwiseoperatortocreatehashcode.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Operationsonbitmappedfields.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Representsacollectionof64booleanonoffflags.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Representsacollectionof64booleanonoffflags.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Representsacollectionof64booleanonoffflags.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Operationsonbitmappedfields.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Usebitwiseoperatortocreatehashcode.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Returnsabytearrayofatleastlength1.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/ConvertingBetweenaBitSetandaByteArray.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/PerformingBitwiseOperationsonaBitVector.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Convertanumbertonegativeandback.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Unsignedshiftingabytevalue.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Signedshifttotheright.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheUnsignedRightShift.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheRightShift.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Leftshiftingasaquickwaytomultiplyby2.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/TheLeftShift.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/BitwiseOperatorAssignments.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Allbitwiseoperatorsinaction.htmhttp://www.java2s.com/Tutorial/Java/0060__Operators/Demonstratethebitwiselogicaloperators.htm
  • 8/9/2019 Computer Science Reference Operators

    21/53

    ^ Bitwise exclusive OR

    >> Shift right

    >>> Shift right zero fill

    > Shift right assignment

    >>>= Shift right zero fill assignment

  • 8/9/2019 Computer Science Reference Operators

    22/53

    00101010 42^ 00001111 15_________00100101 37

    3. 5. 3. Bitwise AND (&)

    public class Main {public static voidmain(String[] a) {

    System.out.println(9 & 7);}

    }//1

    3. 5. 4. Bitwise OR (|)

    public class Main {public static voidmain(String[] a) {

    System.out.println(19 | 7);}

    }//23

    3. 5. 5. Bitwise XOR (^)

    public classMain {public static voidmain(String[] a) {

    System.out.println(9 ^ 7);}

    }//14

    3 . 5. 6. Left shift (

  • 8/9/2019 Computer Science Reference Operators

    23/53

    public class Main {public static voidmain(String[] a) {

    int i = 1;System.out.println(i);

    int j = ~i + 1;

    System.out.println(j);

    i = ~j + 1;System.out.println(i);

    }}/*1-11*/

    3. 5. 8. Demonstrate the bitwise logical operators

    public class MainClass {public static voidmain(String args[]) {

    String binary[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111","1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"

    };int a = 3; // 0 + 2 + 1 or 0011 in binaryint b = 6; // 4 + 2 + 0 or 0110 in binaryint c = a | b;int d = a & b;int e = a ^ b;

    int f = (~a & b) | (a & ~b);int g = ~a & 0x0f;

    System.out.println(" a = " + binary[a]);System.out.println(" b = " + binary[b]);System.out.println(" a|b = " + binary[c]);System.out.println(" a&b = " + binary[d]);System.out.println(" a^b = " + binary[e]);System.out.println("~a&b|a&~b = " + binary[f]);System.out.println(" ~a = " + binary[g]);

    }}

    a = 0011b = 0110a|b = 0111a&b = 0010a^b = 0101~a&b|a&~b = 0101~a = 1100

  • 8/9/2019 Computer Science Reference Operators

    24/53

    3. 5. 9. All bitwise operators in action

    // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002// www.BruceEckel.com. See copyright notice in CopyRight.txt.

    import java.util.Random;

    public class MainClass {public static voidmain(String[] args) {

    Random rand = new Random();int i = rand.nextInt();int j = rand.nextInt();printBinaryInt("-1", -1);printBinaryInt("+1", +1);int maxpos = 2147483647;printBinaryInt("maxpos", maxpos);int maxneg = -2147483648;printBinaryInt("maxneg", maxneg);printBinaryInt("i", i);printBinaryInt("~i", ~i);printBinaryInt("-i", -i);printBinaryInt("j", j);printBinaryInt("i & j", i & j);printBinaryInt("i | j", i | j);printBinaryInt("i ^ j", i ^ j);printBinaryInt("i > 5", i >> 5);printBinaryInt("(~i) >> 5", (~i) >> 5);printBinaryInt("i >>> 5", i >>> 5);

    printBinaryInt("(~i) >>> 5", (~i) >>> 5);

    long l = rand.nextLong();long m = rand.nextLong();printBinaryLong("-1L", -1L);printBinaryLong("+1L", +1L);long ll = 9223372036854775807L;printBinaryLong("maxpos", ll);long lln = -9223372036854775808L;printBinaryLong("maxneg", lln);printBinaryLong("l", l);printBinaryLong("~l", ~l);printBinaryLong("-l", -l);printBinaryLong("m", m);printBinaryLong("l & m", l & m);printBinaryLong("l | m", l | m);printBinaryLong("l ^ m", l ^ m);printBinaryLong("l > 5", l >> 5);printBinaryLong("(~l) >> 5", (~l) >> 5);printBinaryLong("l >>> 5", l >>> 5);printBinaryLong("(~l) >>> 5", (~l) >>> 5);

    }

  • 8/9/2019 Computer Science Reference Operators

    25/53

    static voidprintBinaryInt(String s, int i) {

    System.out.println(s + ", int: " + i + ", binary: ");System.out.print(" ");for (int j = 31; j >= 0; j--)if (((1 = 0; i--)if (((1L 5, int: 28366869, binary:

  • 8/9/2019 Computer Science Reference Operators

    26/53

    00000001101100001101100000010101(~i) >> 5, int: -28366870, binary:

    11111110010011110010011111101010i >>> 5, int: 28366869, binary:

    00000001101100001101100000010101

    (~i) >>> 5, int: 105850858, binary:00000110010011110010011111101010-1L, long: -1, binary:

    1111111111111111111111111111111111111111111111111111111111111111+1L, long: 1, binary:

    0000000000000000000000000000000000000000000000000000000000000001maxpos, long: 9223372036854775807, binary:

    0111111111111111111111111111111111111111111111111111111111111111maxneg, long: -9223372036854775808, binary:

    1000000000000000000000000000000000000000000000000000000000000000l, long: 6929873296403828491, binary:

    0110000000101011110110110000110000001010100111011000111100001011~l, long: -6929873296403828492, binary:

    1001111111010100001001001111001111110101011000100111000011110100-l, long: -6929873296403828491, binary:

    1001111111010100001001001111001111110101011000100111000011110101m, long: -352541115944271612, binary:

    1111101100011011100001011011100100001111011011000000000100000100l & m, long: 6920767123913179392, binary:

    0110000000001011100000010000100000001010000011000000000100000000l | m, long: -343434943453622513, binary:

    1111101100111011110111111011110100001111111111011000111100001111l ^ m, long: -7264202067366801905, binary:

    1001101100110000010111101011010100000101111100011000111000001111l > 5, long: 216558540512619640, binary:

    0000001100000001010111101101100001100000010101001110110001111000(~l) >> 5, long: -216558540512619641, binary:

    1111110011111110101000010010011110011111101010110001001110000111l >>> 5, long: 216558540512619640, binary:

    0000001100000001010111101101100001100000010101001110110001111000(~l) >>> 5, long: 359902211790803847, binary:

    0000010011111110101000010010011110011111101010110001001110000111

    3. 5. 10. Bitwise Operator Assignments

    public class MainClass {public static voidmain(String args[]) {

  • 8/9/2019 Computer Science Reference Operators

    27/53

    int a = 1;int b = 2;int c = 3;

    a |= 4;b >>= 1;

    c

  • 8/9/2019 Computer Science Reference Operators

    28/53

    for(i=0; i 2; // a now contains 8

    System.out.println(a);

    }}

    3. 5. 14. The Unsigned Right Shift

    public class MainClass {public static voidmain(String args[]) {

    int a = -1;a = a >>> 24;

    System.out.println(a);

    }}

    255

    3. 5. 15. Signed shift to the right

  • 8/9/2019 Computer Science Reference Operators

    29/53

    public class Main {public static voidmain(String[] argv) throws Exception {

    byte b = 11;

    System.out.println(b >> 1);}

    }

    3. 5. 16. Unsigned shifting a byte value.

    public class MainClass {static public voidmain(String args[]) {

    char hex[] = {

    '0', '1', '2', '3', '4', '5', '6', '7','8', '9', 'a', 'b', 'c', 'd', 'e', 'f'

    };byte b = (byte) 0xf1;byte c = (byte) (b >> 4);byte d = (byte) (b >>> 4);byte e = (byte) ((b & 0xff) >> 4);System.out.println(" b = 0x"+ hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);

    System.out.println(" b >> 4 = 0x"+ hex[(c >> 4) & 0x0f] + hex[c & 0x0f]);

    System.out.println(" b >>> 4 = 0x"+ hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);

    System.out.println("(b & 0xff) >> 4 = 0x"+ hex[(e >> 4) & 0x0f] + hex[e & 0x0f]);

    }}

    b = 0xf1b >> 4 = 0xffb >>> 4 = 0xff(b & 0xff) >> 4 = 0x0f

    3. 5. 17. Convert a number to negative and back

    public class Main {public static voidmain(String[] a) {

    int i = 1;

    System.out.println(i);int j = ~i + 1;

  • 8/9/2019 Computer Science Reference Operators

    30/53

    System.out.println(j);

    i = ~j + 1;System.out.println(i);

    }}

    /*1-11*/

    3. 5. 18. Performing Bitwise Operations on a Bit Vector

    import java.util.BitSet;

    public class Main {public static voidmain(String[] argv) throws Exception {

    // Create the bitsetBitSet bits = new BitSet();

    // Set a bit onbits.set(2);

    // Retrieving the value of a bitboolean b = bits.get(0);b = bits.get(2);

    // Clear a bitbits.clear(1);

    // Setting a range of bitsBitSet bits2 = new BitSet();bits2.set(1, 4);

    // And'ing two bitsetsbits.and(bits2);

    // Xor'ing two bitsetsbits.xor(bits2);

    // Flip all bits in the bitsetbits.flip(0, bits.length());

    // Andnot'ing two bitsetsbits.andNot(bits2);

    // Or'ing two bitsetsbits.or(bits2);

    }}

    3. 5. 19. Converting Between a BitSet and a Byte Array

  • 8/9/2019 Computer Science Reference Operators

    31/53

    import java.util.BitSet;

    public class Main {public static voidmain(String[] argv) throws Exception {

    System.out.println(fromByteArray(new byte[]{1,2,3}));}

    // Returns a bitset containing the values in bytes.public static BitSet fromByteArray(byte[] bytes) {

    BitSet bits = new BitSet();for (int i = 0; i < bytes.length * 8; i++) {if ((bytes[bytes.length - i / 8 - 1] & (1 0) {bits.set(i);

    }}return bits;

    }}

    //{0, 1, 9, 16}

    3. 5. 20. Returns a byte array of at least length 1

    import java.util.BitSet;

    public class Main {public static voidmain(String[] argv) throws Exception {

    BitSet bitset = new BitSet();bitset.set(1);System.out.println(toByteArray(bitset));

    }

    public static byte[] toByteArray(BitSet bits) {byte[] bytes = new byte[bits.length() / 8 + 1];for (int i = 0; i < bits.length(); i++) {if (bits.get(i)) {bytes[bytes.length - i / 8 - 1] |= 1

  • 8/9/2019 Computer Science Reference Operators

    32/53

    System.out.print((hc & 0x80000000) != 0 ? '1' : '0');hc

  • 8/9/2019 Computer Science Reference Operators

    33/53

    */public BitField(int mask) {

    _mask = mask;int count = 0;int bit_pattern = mask;

    if (bit_pattern != 0) {while ((bit_pattern & 1) == 0) {count++;bit_pattern >>= 1;

    }}_shift_count = count;

    }

    /*** Obtains the value for the specified BitField, appropriately* shifted right.** Many users of a BitField will want to treat the specified

    * bits as an int value, and will not want to be aware that the* value is stored as a BitField (and so shifted left so many* bits).** @see #setValue(int,int)* @param holder the int data containing the bits we're interested* in* @return the selected bits, shifted right appropriately*/

    public int getValue(int holder) {return getRawValue(holder) >> _shift_count;

    }

    /*** Obtains the value for the specified BitField, appropriately* shifted right, as a short.** Many users of a BitField will want to treat the specified* bits as an int value, and will not want to be aware that the* value is stored as a BitField (and so shifted left so many* bits).** @see #setShortValue(short,short)* @param holder the short data containing the bits we're* interested in* @return the selected bits, shifted right appropriately*/

    public short getShortValue(short holder) {return (short) getValue(holder);

    }

    /*** Obtains the value for the specified BitField, unshifted.** @param holder the int data containing the bits we're* interested in

  • 8/9/2019 Computer Science Reference Operators

    34/53

    * @return the selected bits*/

    public int getRawValue(int holder) {return holder & _mask;

    }

    /*** Obtains the value for the specified BitField, unshifted.** @param holder the short data containing the bits we're* interested in* @return the selected bits*/

    public short getShortRawValue(short holder) {return (short) getRawValue(holder);

    }

    /*** Returns whether the field is set or not.*

    * This is most commonly used for a single-bit field, which is* often used to represent a boolean value; the results of using* it for a multi-bit field is to determine whether *any* of its* bits are set.** @param holder the int data containing the bits we're interested* in* @return true if any of the bits are set,* else false*/

    public boolean isSet(int holder) {return (holder & _mask) != 0;

    }

    /*** Returns whether all of the bits are set or not.** This is a stricter test than {@link #isSet(int)},* in that all of the bits in a multi-bit set must be set* for this method to return true.** @param holder the int data containing the bits we're* interested in* @return true if all of the bits are set,* else false*/

    public boolean isAllSet(int holder) {

    return (holder & _mask) == _mask;}

    /*** Replaces the bits with new values.** @see #getValue(int)* @param holder the int data containing the bits we're* interested in

  • 8/9/2019 Computer Science Reference Operators

    35/53

    * @param value the new value for the specified bits* @return the value of holder with the bits from the value* parameter replacing the old bits*/

    public int setValue(int holder, int value) {return (holder & ~_mask) | ((value

  • 8/9/2019 Computer Science Reference Operators

    36/53

    return (byte) clear(holder);}

    /*** Sets the bits.*

    * @param holder the int data containing the bits we're* interested in* @return the value of holder with the specified bits set* to 1*/

    public int set(int holder) {return holder | _mask;

    }

    /*** Sets the bits.** @param holder the short data containing the bits we're* interested in

    * @return the value of holder with the specified bits set* to 1*/

    public short setShort(short holder) {return (short) set(holder);

    }

    /*** Sets the bits.** @param holder the byte data containing the bits we're* interested in*

    * @return the value of holder with the specified bits set* to 1*/

    public byte setByte(byte holder) {return (byte) set(holder);

    }

    /*** Sets a boolean BitField.** @param holder the int data containing the bits we're* interested in* @param flag indicating whether to set or clear the bits* @return the value of holder with the specified bits set or

    * cleared*/

    public int setBoolean(int holder,boolean flag) {return flag ? set(holder) : clear(holder);

    }

    /*** Sets a boolean BitField.*

  • 8/9/2019 Computer Science Reference Operators

    37/53

    * @param holder the short data containing the bits we're* interested in* @param flag indicating whether to set or clear the bits* @return the value of holder with the specified bits set or* cleared*/

    public short setShortBoolean(short holder,boolean flag) {return flag ? setShort(holder) : clearShort(holder);}

    /*** Sets a boolean BitField.** @param holder the byte data containing the bits we're* interested in* @param flag indicating whether to set or clear the bits* @return the value of holder with the specified bits set or* cleared*/

    public byte setByteBoolean(byte holder,boolean flag) {

    return flag ? setByte(holder) : clearByte(holder);}

    }

    3. 5. 23. Represents a collection of 64 boolean (on/off) flags.

    /** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements. See the NOTICE file distributed with* this work for additional information regarding copyright ownership.

    * The ASF licenses this file to You under the Apache License, Version 2.0* (the "License"); you may not use this file except in compliance with* the License. You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/

    import java.io.Serializable;

    /*** Represents a collection of 64 boolean (on/off) flags. Individual flags* are represented by powers of 2. For example,
    * Flag 1 = 1
    * Flag 2 = 2
    * Flag 3 = 4
    * Flag 4 = 8

    * or using shift operator to make numbering easier:

  • 8/9/2019 Computer Science Reference Operators

    38/53

    * Flag 1 = 1