






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
r rtgrgrfgfdgfdgfgff fgffgfgf
Typology: Exercises
1 / 10
This page cannot be seen from the preview
Don't miss anything!
Arithmetic Operators Arithmetic Operators Relational Operators Relational Operators Bitwise Operators Bitwise Operators Logical Operators Logical Operators Assignment Operators Assignment Operators Misc Operators Misc Operators
OperatorOperator (^) DescriptionDescription (^) ExampleExample
OperatorOperator^ DescriptionDescription^ ExampleExample & (bitwise and) & (bitwise and) Binary AND Operator copies a bit to the result if it exists in both operands.Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100(A & B) will give 12 which is 0000 1100 | (bitwise or)| (bitwise or)^ Binary OR Operator copies a bit if it exists in either operand.Binary OR Operator copies a bit if it exists in either operand.^ (A | B) will give 61 which is 0011 1101(A | B) will give 61 which is 0011 1101 ^ (bitwise XOR) ^ (bitwise XOR) Binary XOR Operator copies the bit if it is set in one operand but not both.Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001(A ^ B) will give 49 which is 0011 0001 ~ (bitwise compliment) ~ (bitwise compliment) Binary Ones Complement Operator is unary and has the effect of 'flipping'Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. bits. (~A ) will give -61 which is 1100 0011 in 2's (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. complement form due to a signed binary number. << (left shift) << (left shift) Binary Left Shift Operator. The left operands value is moved left by theBinary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000A << 2 will give 240 which is 1111 0000
(right shift) >> (right shift) Binary Right Shift Operator. The left operands value is moved right by theBinary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. number of bits specified by the right operand. A >> 2 will give 15 which is 1111A >> 2 will give 15 which is 1111
(zero fill right shift) >>> (zero fill right shift) Shift right zero fill operator. The left operands value is moved right by the Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up number of bits specified by the right operand and shifted values are filled up with zeros. with zeros. A >>>2 will give 15 which is 0000 1111A >>>2 will give 15 which is 0000 1111 The Logical OperatorsThe Logical Operators
OperatorOperator^ DescriptionDescription^ ExampleExample && (logical and)&& (logical and)^ Called Logical AND operator. If both the operands are non-zero, then the conditionCalled Logical AND operator. If both the operands are non-zero, then the condition becomes true. becomes true. (A && B) is false(A && B) is false || (logical or) || (logical or) Called Logical OR Operator. If any of the two operands are non-zero, then the conditionCalled Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. becomes true. (A || B) is true(A || B) is true ! (logical not)! (logical not) Called Logical NOT Operator. Use to reverses the logical state of its operand. If aCalled Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. condition is true then Logical NOT operator will make false. !(A && B) is true!(A && B) is true The Assignment OperatorsThe Assignment Operators
variable x = (expression)? value if true : value if falsevariable x = (expression)? value if true : value if false
public public classclass TestTest {{ public public staticstatic voidvoid (^) mainmain((StringString (^) argsargs[])[]) (^) {{ int int aa,, bb;; a a (^) == 1010 ;; b b == ((aa ==== 11 )) ?? 2020 :: 3030 ;; System System..outout..printlnprintln(( "Value of b is : ""Value of b is : " ++ (^) bb );); b b (^) == ((aa ==== 1010 )) ?? 2020 :: (^3030) ;; System System..outout..printlnprintln(( "Value of b is : ""Value of b is : " ++ (^) bb );); } } } }
Value of b is : 30 Value of b is : 30 Value of b is : 20 Value of b is : 20 instanceof Operatorinstanceof Operator
( Object reference variable ) instanceof ( Object reference variable ) instanceof (class/interface type)(class/interface type)
Live Demo Live Demo
publicpublic classclass TestTest {{ public public staticstatic voidvoid (^) mainmain((StringString (^) argsargs[])[]) (^) {{ String String namename == "James""James";; // following will return true since name is type of String // following will return true since name is type of String boolean boolean (^) resultresult (^) == namename (^) instanceofinstanceof StringString;; System System..outout..printlnprintln(( resultresult );); } } } }
true true
class class VehicleVehicle {}{} public public classclass CarCar extendsextends VehicleVehicle {{ public public staticstatic voidvoid mainmain((StringString argsargs[])[]) {{ Vehicle Vehicle aa == newnew CarCar();(); boolean boolean (^) resultresult (^) == aa instanceofinstanceof CarCar;; System System..outout..printlnprintln(( resultresult );); } } } }
Live Demo Live Demo Live Demo Live Demo
CategoryCategory^ OperatorOperator^ AssociativityAssociativity Postfix Postfix expression++ expression--expression++ expression-- Left to rightLeft to right Unary Unary ++expression –-expression +expression –expression ~ !++expression –-expression +expression –expression ~! Right to leftRight to left Multiplicative Multiplicative * / %* / % Left to rightLeft to right AdditiveAdditive^ + -+ -^ Left to rightLeft to right Shift Shift (^) << >> >>><< >> >>> Left to rightLeft to right Relational Relational < >< > <= >= instanceof<= >= instanceof Left to rightLeft to right EqualityEquality == !=== != Left to rightLeft to right Bitwise ANDBitwise AND && Left to rightLeft to right Bitwise XOR Bitwise XOR ^^ Left to rightLeft to right Bitwise ORBitwise OR^ ||^ Left to rightLeft to right Logical ANDLogical AND^ &&&&^ Left to rightLeft to right Logical ORLogical OR^ ||||^ Left to rightLeft to right Conditional Conditional ?:?: Right to leftRight to left Assignment Assignment = += -= *= /= %= ^= |= <<= >>= >>>== += -= *= /= %= ^= |= <<= >>= >>>= Right to leftRight to left What is Next?What is Next?