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 3

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 shift operations

  • Shift the bits of the first integer operand the number of bits specified by the second integer operand. The operator used determines the direction of the shift and whether the sign of the number is to be maintained.

  • The maximum number of bit positions that can be shifted is 31 bits. If the second operand specifies a shift value greater than 31, only its right-most 5 bits will be used as the shift value. In other words, a shift factor of 32 is the same as a shift factor of 0.

  • Use the << (left shift), >> (signed right shift), and >>> (unsigned right shift) operators. The rules for these operations are as follows:

Operation Rule

<<

Shifts the first operand to the left the number of bits specified by the right-most 5 bits of the second operand. The low-order bit of the first operand is replaced with binary 0.

>>

Shifts the first operand to the right the number of bits specified by the right-most 5 bits of the second operand. The sign bit of the first operand is shifted and replaced with its original value (so the sign is maintained).

>>>

Shifts the first operand to the right the number of bits specified by the right-most 5 bits of the second operand. The sign bit of the first operand is shifted and replaced with binary 0.

For example, if

byte x = 7; // Binary value: 0000 0111 Hex value: 07
byte y;

this table shows the result of executing three unrelated statements. The cast is needed in order to store the int value that results from the operation.

Statement Binary Result Hexadecimal Decimal

y = (byte)(x >> 2);

0000 0001 01 01

y = (byte)(x << 2);

0001 1100 1C 28

y = (byte)(x >>> 3);

0000 0000 00 00

Example: The following program can be run to test bitwise shift operations.

public class App {
public static void main(String[] args) {

// Variables to be read from the user

int first;
int second;

// Prompt for and read the two integers

System.out.print("First integer: ");
first = Keyboard.readInt();
System.out.print("Second integer: ");
second = Keyboard.readInt();

// Display the results of bitwise shift operations

System.out.println(" " + first + " << " + second + " = " +
(first << second));
System.out.println(" " + first + " >> " + second + " = " +
(first >> second));
System.out.println(" " + first + " >>> " + second + " = " +
(first >>> second));
}
}

Notes:

  1. Program results are displayed in decimal. To really understand what is happening, work out the equivalent binary and hexadecimal values.

  2. Be sure to run the program several times with different integer values.


 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