Difference between revisions of "CSC231 Bitwise Logical Operators in Java"
(Created page with "--~~~~ ---- <source lang="java"> public class Demo1 { final static byte START = 0x01; final static byte PHASE1 = 0x02; final static byte PHASE2 = 0x04; final static byte PH...") |
|||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 23:38, 9 October 2012 (EDT) | --[[User:Thiebaut|D. Thiebaut]] 23:38, 9 October 2012 (EDT) | ||
---- | ---- | ||
+ | =Source= | ||
<source lang="java"> | <source lang="java"> | ||
Line 35: | Line 36: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | =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 | ||
+ | |||
<br /> | <br /> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
[[Category:CSC231]][[Category:Java]] | [[Category:CSC231]][[Category:Java]] |
Revision as of 23:39, 9 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 = " + String.format( "%8s", Integer.toBinaryString(status) ).replace(' ', '0') );
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 = " + String.format( "%8s", Integer.toBinaryString(status) ).replace(' ', '0') );
if ( (status & DONE) != 0)
break;
}
}
}
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