Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

JavaFAQ Home » Java Lessons by Jon Huhtala Go to all tips in Java Lessons by Jon Huhtala


Bookmark and Share
All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46

The Java Lesson 7

Bitwise operations: overview with detailed examples

1. Logical bitwise operations
2. The complement operator
3. Bitwise shift operations
4. Bitwise assignment operations andr review questions

Bitwise assignment operations

  • All bitwise operators except the unary complement (~) can be combined with assignment as long as the first operand is an integer variable. For example,

int x = 23;
x <<= 2;
System.out.println("Now x is " + x);

will display "Now x is 92" because 0000 0017 shifted 2 bits to the left becomes 0000 005C.

  • Do not require casts to prevent compile errors. Possible data loss is ignored (so be careful). For example,

byte x = 127;
x |= 256;
System.out.println("Now x is " + x);

will display "Now x is 127" because 7F OR 0000 0100 results in 0000 017F (decimal 383) but only the right-most 8 bits are assigned to byte variable x.

More practice

The Java ranch tutorial Cat and Mouse Games with Bits ends with an applet you can use to practice bitwise operations.

Review questions

  1. If x is short variable with a decimal value of 27, what data type and decimal value will result from executing the following expression?

x & 8

  1. a short with a value of 8

  2. an int with a value of 8

  3. a short with a value of 27

  4. an int with a value of 27

  5. a short with a value of 19

  1. If someNumber is an int variable having some value, what value will it have after executing the following statement?

someNumber ^= someNumber;

  1. the statement will not compile

  2. the statement will compile but an error will occur at run time

  3. 0

  4. -1

  5. the value cannot be determined from the information given

  1. If amount is a short variable with a decimal value of 6, which of the following statements will multiply amount by four and store the result in a short variable named newAmount? (choose two)

  1. newAmount = (short) (amount << 2);

  2. newAmount = amount << 2;

  3. newAmount = (short) (amount >> 2);

  4. newAmount = amount >> 2;

  5. newAmount = (short) (amount << 34);

  1. If x is an int variable with an initial value of 7, what value will it have after executing the following?

x >>>= (x ^ 6);

  1. 0

  2. 3

  3. 14

  4. 448

  5. the statement will not compile


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy