Don't confuse &&, which is the short-circuit logical and, with &, which is the uncommon bitwise and. Altho the bitwise and can also be used with boolean operands, this is extremely rare and is almost always a programming error.
Showing posts with label Bitwise. Show all posts
Showing posts with label Bitwise. Show all posts
Sunday, 22 May 2011
&& vs & operator
Thursday, 21 April 2011
Storing integral types in java
All the integer types in Java technology are internally stored in two's complement. In two's complement, positive numbers have their corresponding binary representation.
For negative numbers see - 2's complement of negative numbers in java
Sunday, 17 April 2011
Binary representation of negative numbers in java : 2's complement
Negative numbers in Java are represented using 2's complement. As we know that integers in Java occupy 4 bytes so to understand how a negative integer (say -4) is represented internally in Java, we first need to find the binary equivalent of the positive value of the integer (in this case 4) and subsequently by finding the 2's complement of that binary representation.
Okay, so how do find 2's complement of a binary number? Simply by adding '1' to the 1's complement of that number. But, how to find 1's complement of a binary number then? Just by reverting the bits of the number i.e., changing 1s to 0s and 0s to 1s. An example may of of some help here.
Example
int i = -4;
...
Step #1: Binary Equivalent of the positive value (4 in this case)
0000 0000 0000 0000 0000 0000 0000 0100
Step #2: 1's complement of the binary rep of 4 by inverting the bits
1111 1111 1111 1111 1111 1111 1111 1011
Step #3: Finding 2's complement by adding 1 to the corresponding 1's complement
1111 1111 1111 1111 1111 1111 1111 1011
0000 0000 0000 0000 0000 0000 0000 0001
---------------------------------------
1111 1111 1111 1111 1111 1111 1111 1100
Thus, we see that integer -4 is represented by the binary sequence (1111 1111 1111 1111 1111 1111 1111 1100) in Java.
Once we have an understanding of how the numbers are represented internally, bit-level manipulation becomes easily understandable, which otherwise is obviously one of the hardest things in Java (or any other language supporting that) to visualize.
Okay, so how do find 2's complement of a binary number? Simply by adding '1' to the 1's complement of that number. But, how to find 1's complement of a binary number then? Just by reverting the bits of the number i.e., changing 1s to 0s and 0s to 1s. An example may of of some help here.
Example
int i = -4;
...
Step #1: Binary Equivalent of the positive value (4 in this case)
0000 0000 0000 0000 0000 0000 0000 0100
Step #2: 1's complement of the binary rep of 4 by inverting the bits
1111 1111 1111 1111 1111 1111 1111 1011
Step #3: Finding 2's complement by adding 1 to the corresponding 1's complement
1111 1111 1111 1111 1111 1111 1111 1011
0000 0000 0000 0000 0000 0000 0000 0001
---------------------------------------
1111 1111 1111 1111 1111 1111 1111 1100
Thus, we see that integer -4 is represented by the binary sequence (1111 1111 1111 1111 1111 1111 1111 1100) in Java.
Once we have an understanding of how the numbers are represented internally, bit-level manipulation becomes easily understandable, which otherwise is obviously one of the hardest things in Java (or any other language supporting that) to visualize.
Tuesday, 26 October 2010
Bitwise Operators - Uses
- Efficient storage.
- Status bits. Eg, Mouse event key mask.
- Eg, representing Connect-Four board.
- Packing or unpacking values into a single int/long.
A common use of the bitwise operators (shifts with ands to extract values and ors to add values) is to work with multiple values that have been encoded in one int. Bit-fields are another way to do this. For example, let's say you have the following integer variables: age (range 0-127), gender (range 0-1), height (range 0-128). These can be packed and unpacked into/from one short (two-byte integer) like this (or many similar variations).//define the variablesint age, gender, height;
short packed_info;
. . .
// packing
packed_info = (((age << 1) | gender) << 7) | height;
. . .
// unpacking
height = packed_info & 0x7f;
gender = (packed_info >>> 7) & 1;
age = (packed_info >>> 8); - Setting flag bits
Some library functions take an int that contains bits, each of which represents a true/false (boolean) value. This saves a lot of space and can be fast to process.
- Efficient computation
- On some (esp old) machines, shifting is faster than multiplying or dividing by powers of two.
y = x << 3; // Assigns 8*x to y.
y = (x << 2) + x; // Assigns 5*x to y. - Flipping between on and off with xor
Sometimes xor is used to flip between 1 and 0.x = x ^ 1; // Or the more cryptic x ^= 1;
In a loop that will change x alternately between 0 and 1. - On some (esp old) machines, shifting is faster than multiplying or dividing by powers of two.
- Problems
- How could you use bit operations to test whether a number is odd?
- How would you multiply by 256?
- How would you swap 2 integers, without using temporary variable?
- How could you use bit operations to test whether a number is odd?
Bitwise Operators
Java's bitwise operators operate on individual bits of integer (int and long) values. If an operand is shorter than an int, it is promoted to int before doing the operations.
It helps to know how integers are represented in binary. For example the decimal number 3 is represented as 11 in binary and the decimal number 5 is represented as 101 in binary.
Negative integers are store in two's complement form. For example, -4 is 1111 1111 1111 1111 1111 1111 1111 1100.
It helps to know how integers are represented in binary. For example the decimal number 3 is represented as 11 in binary and the decimal number 5 is represented as 101 in binary.
Negative integers are store in two's complement form. For example, -4 is 1111 1111 1111 1111 1111 1111 1111 1100.
Bitwise operators can fall under 2 categories.>>,>>>,<< operators are called shift operators, which is covered in this post. Other operators are ~,&,| and ^ operators.
| ~ | Not operator |
| ^ | Xor or Exclusive OR |
| | | OR |
| & | And operator |
| >> | Right shift |
| >>> | Unsigned right shift |
| << | left shift |
Note that operands for Bitwise operators must be numeric datatypes(char, short, int, or long).
Examples - The bitwise operators
| Operator | Usage | Example | Result | Reason |
| & or and Operator | a & b | 3 & 5 | 1 | 1 if both bits are 1. |
| | OR operator | a | b | 3 | 5 | 7 | 1 if either bit is 1. |
| ^ or xor operator | a ^ b | 3 ^ 5 | 6 | 1 if both bits are different. |
| ~ or not operator | ~a | ~3 | 4 | Inverts the bits. |
| Left shift | n << p | 3 <<< 2 | 12 | Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions. |
| Right shift | n >> p | 5 >> 2 | 1 | Shifts the bits of n right p positions. If n is a 2's complement signed number, the sign bit is shifted into the high-order positions. |
| Unsigned right shift | n >>> p | -4 >>> 28 | 15 | Shifts the bits of n right p positions. Zeros are shifted into the high-order positions. |
Subscribe to:
Comments (Atom)