会计考友 发表于 2012-8-4 12:44:44

JAVA语言基础之运算符4

程序例
public class BitwiseDemo
{   static final int VISIBLE = 1;      //二进制位为:00000001
    static final int DRAGGABLE = 2;    //二进制位为:00000010
    static final int SELECTABLE = 4;   //二进制位为:00000100
    static final int EDITABLE = 8;   //二进制位为:00001000
    public static void main(String[] args)
    {   int flags = 0;                        //00000000   
      flags = flags | VISIBLE;      
//               00000000
//            00000001                        此时Flags=00000001
      flags = flags | DRAGGABLE;         
//                  00000001
//       +       00000010                      此时Flags=00000011
      if ((flags & VISIBLE) == VISIBLE)
            {
//                   00000011
//            * 00000001             结果flags=00000001
            if ((flags & DRAGGABLE) == DRAGGABLE)
                     {
//                   00000011
//            * 00000010             结果flags=00000010
               System.out.println("Flags are Visible and Draggable.");
            }
      }
      flags = flags | EDITABLE;
//                   00000010
//            + 00001000            结果flags=00001010         
      if ((flags & EDITABLE) == EDITABLE)
             {

会计考友 发表于 2012-8-4 12:44:45

JAVA语言基础之运算符4

//                   00001010
//            * 00001000             结果flags=00001000                              
            System.out.println("Flags are now also Editable.");
      }
    }
}
l         条件表达式
class TriOperator
{       public static void main(String args[])
   {       int a = 0;
   int b = 0;
      boolean bFlag = false;
      int c=bFlag ? a++:b++;             //由于b++为后置运算,先赋给c再增1
      System.out.println("a="+a+" b="+b+" Result=" + c);
       bFlag = true;
       c=bFlag ? a ++:b++;    //由于a++为后置运算,先赋给c再增1
       System.out.println("a="+a+" b="+b+" Result=" + c);
   }
}</p>http://www.examw.com/java/Files/2012-7/1.jpg
页: [1]
查看完整版本: JAVA语言基础之运算符4