001package beginner;
002
003import com.sandwich.koan.Koan;
004import static com.sandwich.koan.constant.KoanConstants.__;
005import static com.sandwich.util.Assert.assertEquals;
006
007public class AboutOperators {
008
009        @Koan
010        public void plusPlusVariablePlusPlus(){
011                int i = 1;
012                assertEquals(++i, __);
013                assertEquals(i, __);
014                assertEquals(i++, __);
015                assertEquals(i, __);
016        }
017        
018        @Koan
019        public void shortCircuit() {
020                int i = 1;
021                int a = 6; // Why did we use a variable here?
022                // What happens if you replace 'a' with '6' below?
023                // Try this with an IDE like Eclipse...
024                if ( (a < 9 ) || (++i < 8) )  i = i + 1;
025                assertEquals(i, __);
026        }
027        
028        @Koan
029        public void fullAnd(){
030                int i = 1;
031                if ( true & (++i < 8) )  i = i + 1;
032                assertEquals(i, __);
033        }
034        
035        @Koan
036        public void shortCircuitAnd(){
037                int i = 1;
038                if ( true && (i < -28) )  i = i + 1;
039                assertEquals(i, __);
040        }
041        
042        @Koan
043        public void aboutXOR() {
044                int i = 1;
045                int a = 6;
046                if ( (a < 9 ) ^ false)  i = i + 1;
047                assertEquals(i, __);
048        }
049        
050        @Koan
051        public void dontMistakeEqualsForEqualsEquals() {
052                int i = 1;
053                boolean a = false;
054                if (a = true) i++;
055                assertEquals(a, __);
056                assertEquals(i, __);
057                // How could you write the condition 'with a twist' to avoid this trap?
058        }
059        
060        @Koan
061        public void aboutBitShiftingRightShift() {
062                int rightShift = 8;
063                rightShift = rightShift >> 1; 
064                assertEquals(rightShift, __);
065        }
066        
067        @Koan
068        public void aboutBitShiftingLeftShift() {
069                int leftShift = 0x80000000; // Is this number positive or negative?
070                leftShift = leftShift << 1;
071                assertEquals(leftShift, __);
072        }
073        
074        @Koan
075        public void aboutBitShiftingRightUnsigned() {
076                int rightShiftNegativeStaysNegative = 0x80000000;
077                rightShiftNegativeStaysNegative = rightShiftNegativeStaysNegative >> 4;
078                assertEquals(rightShiftNegativeStaysNegative, __);
079                int unsignedRightShift = 0x80000000; // always fills with 0
080                unsignedRightShift >>>= 4; // Just like +=
081                assertEquals(unsignedRightShift, __);
082        }
083        
084}