Chap 4 - Operators Flashcards
int a = 1
int b = ++a
int c = a++
System.out.println(“a = “ + a);
System.out.println(“b = “ + b);
System.out.println(“c = “ + c);
a = 3
b = 2
c = 2
c sets to a before it’s incremented
What does % operator do?
Called modulus operator
Returns the remainder of a division operation
What is Twos Compliment?
Encoding for negative numbers
For binary code, change 42 to -42.
00101010 = 42
11010110 = -42
List steps for changing number to its negative in binary
1) invert (0s to 1s and visa versa)
2) add 1
Which bit tells you the sign of an integer?
highest order bit, on the left
Bitwise operators, explain
&
|
~
AND, OR, NOT
AND says if both are 1
OR says if either are 1
NOT inverts the 1<>0
Write the bitwise OR operator
|
00101010
| 00001111
—————
00101111
What is bitwise XOR operator and what does it do?
Says if exactly one is 1/TRUE
00101010
^ 00001111
—————
00100101
What does left shift operator «_space;do?
shifts all bits in a value to the left a specified number of times
value «_space;num
Bye and short values are promoted to what when an expression is evaluated?
int
Each «_space;has the effect of what on the original value?
Doubling, except when a 1 gets shifted off the right, then its a problem
byte a = 64, b;
int i;
i = a «_space;2;
b = (byte) (a «_space;2);
// print i and b
i = 256
b = 0
–
0100 0000 is 64. Shift left 2 is
1 0000 0000
That number is 256 as an int
Buts its 0 as a byte b/c the leftmost 1 is shifted out / too many numbers for the variable size
Explain what happens with»_space; operator, how the top bit is filled, and what the outcome is
Bitwise operator that shifts each bit to the right.
Top bit is filled with the previous content of the top bit
Outcome divides value by two and discards remainder
What does»_space;> do?
unsigned shift-right operator
Shifts bits to the right and 0 into the top bit, unlike»_space; which maintains the sign for the top bit