Difference between revisions of "CSC231 Bitwise Logical Operators in Java"

From dftwiki3
Jump to: navigation, search
(Source)
Line 5: Line 5:
  
 
public class Demo1 {
 
public class Demo1 {
final static byte START  = 0x01;
+
    final static byte START  = 0x01;
final static byte PHASE1 = 0x02;
+
    final static byte PHASE1 = 0x02;
final static byte PHASE2 = 0x04;
+
    final static byte PHASE2 = 0x04;
final static byte PHASE3 = 0x08;
+
    final static byte PHASE3 = 0x08;
final static byte DONE  = 0x10;
+
    final static byte DONE  = 0x10;
 +
   
 +
    /**
 +
    * @param args
 +
    */
 +
    public static void main(String[] args) {
 +
byte status = START;
 +
System.out.println( "Status = " + toBinary8( status ) );
 
 
/**
+
for ( int i = 0; i< 100; i++ ) {
* @param args
+
    if ( Math.random() < 0.1 ) status = (byte) (status | PHASE1);
*/
+
    else if ( Math.random() < 0.1 ) status = (byte) (status ^ PHASE1);
public static void main(String[] args) {
+
    else if ( Math.random() < 0.1 ) status = (byte) (status ^ PHASE2);
byte status = START;
+
    else if ( Math.random() < 0.1 ) status = (byte) (status ^ PHASE3);
System.out.println( "Status = " + String.format( "%8s", Integer.toBinaryString(status) ).replace(' ', '0') );
+
    else if ( Math.random() < 0.1 ) status = (byte) (status ^ DONE);
+
    System.out.println( "Status = " + toBinary8( status ) );
for ( int i = 0; i< 100; i++ ) {
+
    if ( (status & DONE) != 0)
if ( Math.random() < 0.1 ) status = (byte) (status | PHASE1);
+
break;
else if ( Math.random() < 0.1 ) status = (byte) (status ^ PHASE1);
+
}
else if ( Math.random() < 0.1 ) status = (byte) (status ^ PHASE2);
+
    }
else if ( Math.random() < 0.1 ) status = (byte) (status ^ PHASE3);
+
else if ( Math.random() < 0.1 ) status = (byte) (status ^ DONE);
+
    private static String toBinary8( byte x ) {
System.out.println( "Status = " + String.format( "%8s", Integer.toBinaryString(status) ).replace(' ', '0') );
+
return  String.format( "%8s", Integer.toBinaryString(x) ).replace(' ', '0');
if ( (status & DONE) != 0)
+
    }
break;
 
}
 
 
}
 
 
 
 
}
 
}
  
Line 36: Line 38:
 
<br />
 
<br />
 
<br />
 
<br />
 +
 
=Example of Output=
 
=Example of Output=
 
  Status = 00000001
 
  Status = 00000001

Revision as of 10:24, 10 October 2012

--D. Thiebaut 23:38, 9 October 2012 (EDT)


Source

public class Demo1 {
    final static byte START  = 0x01;
    final static byte PHASE1 = 0x02;
    final static byte PHASE2 = 0x04;
    final static byte PHASE3 = 0x08;
    final static byte DONE   = 0x10;
    
    /**
     * @param args
     */
    public static void main(String[] args) {
	byte status = START;
	System.out.println( "Status = " + toBinary8( status ) );
	
	for ( int i = 0; i< 100; i++ ) {
	    if ( Math.random() < 0.1 ) status = (byte) (status | PHASE1);
	    else if ( Math.random() < 0.1 ) status = (byte) (status ^ PHASE1);
	    else if ( Math.random() < 0.1 ) status = (byte) (status ^ PHASE2);
	    else if ( Math.random() < 0.1 ) status = (byte) (status ^ PHASE3);
	    else if ( Math.random() < 0.1 ) status = (byte) (status ^ DONE);
	    System.out.println( "Status = " + toBinary8( status ) );
	    if ( (status & DONE) != 0) 
		break;
	}	
    }
 
    private static String toBinary8( byte x ) {
	return  String.format( "%8s", Integer.toBinaryString(x) ).replace(' ', '0');
    }
}



Example of Output

Status = 00000001
Status = 00000001
Status = 00000001
Status = 00000001
Status = 00000001
Status = 00000001
Status = 00000011 
Status = 00001011
Status = 00001011
Status = 00001011
Status = 00011011