Bitwise and Bit Shift Operators example in Java
By: Grenfel Printer Friendly Format
The unary bitwise complement operator "~
" inverts a
bit pattern; it can be applied to any of the integral types, making every
"0" a "1" and every "1" a "0". For
example, a byte
contains 8 bits; applying this operator to a
value whose bit pattern is "00000000" would change its pattern to
"11111111".
The signed left shift operator "<<
" shifts a
bit pattern to the left, and the signed right shift operator ">>
"
shifts a bit pattern to the right. The bit pattern is given by the left-hand
operand, and the number of positions to shift by the right-hand operand. The
unsigned right shift operator ">>>
" shifts a
zero into the leftmost position, while the leftmost position after ">>"
depends on sign extension.
The bitwise &
operator performs a bitwise AND operation.
The bitwise ^
operator performs a bitwise exclusive OR
operation.
The bitwise |
operator performs a bitwise inclusive OR
operation.
The following program, BitDemo
, uses the bitwise AND operator
to print the number "2" to standard output.
class BitDemo { public static void main(String[] args) { int bitmask = 0x000F; int val = 0x2222; System.out.println(val & bitmask); // prints "2" } }
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Archived Comments
1. For more clear and easy concept about bitwise left
View Tutorial By: zhbd at 2011-09-29 04:48:13