How to purpose bitwise Operator inwards Java? Power of 2 example

In this Java Programming tutorial yous volition larn how to cheque if number is Power of ii using bitwise operator. Main role or this computer program is to instruct yous how to exercise bit-wise operators similar  bitwise AND (&)  in Java. Influenza A virus subtype H5N1 number is said to survive ability of ii if all its prime number factors are 2, but inwards binary earth things industrial plant lilliputian differently. If yous cause got read hacker's delight mass thus yous know that at that spot are several technique to cheque if a number is ability of ii or not, 1 of them is performing bit-wise AND functioning betwixt number in addition to number -1, if number is nil thus number is ability of two. Just remember, hither nosotros are doing binary subtraction in addition to non decimal one. In bitwise operator, each flake is used inwards evaluation for instance if yous exercise bitwise AND thus each flake of both operand volition acquire through AND functioning in addition to number volition comprise 1 solely if both bits are 1 otherwise zero. I volition explicate how precisely this computer program work, but let's kickoff reckon the computer program itself.



Java Program to Check if Number is Power of Two

Here is my solution of this problem. Of-course at that spot are many ways to solve this problem, including yesteryear using arithmetics operator equally shown inwards my earlier post, but I cause got purposefully used bit-wise operator to present how slowly in addition to fast is to devise an algorithm using bit-wise operators. This computer program is a real expert instance of learning bit-wise operator inwards Java.



/**  * Java Program to cheque if a number is ability of ii or not.  *  * @author Javin  */ public class PowerOfTwo{      public static void main(String args[]) {         System.out.printf("is %d ability of Two? %b%n", 2, isPowerofTwo(2));         System.out.printf("is %d ability of Two? %b%n", 4, isPowerofTwo(4));         System.out.printf("is %d ability of Two? %b%n", 5, isPowerofTwo(5));         System.out.printf("is %d ability of Two? %b%n", 1, isPowerofTwo(1));         System.out.printf("is %d ability of Two? %b%n", -1, isPowerofTwo(-1));     }      /*      * @return true, if number is ability of two, otherwise false.      */     public static boolean isPowerofTwo(int number) {         return (number & (number - 1)) == 0;     }  }  Output is 2 ability of Two? true is 4 ability of Two? true is 5 ability of Two? false is 1 ability of Two? true is -1 ability of Two? false

Explanation :
Though this solution is just a beautiful 1 liner, it may accept closed to fourth dimension for yous to sympathize what's happening. To arrive simple, let's start alongside the code itself number & (number - 1)) == 0, So what are nosotros doing here? Basically wee are checking if number & (number - 1) is equal to nil thus number is ability of two. We are doing ii functioning inwards this code, kickoff is binary subtraction in addition to bit is bitwise AND operation. Let's kickoff reckon how bitwise AND operator works. As cite advise it performs AND functioning on every unmarried flake of both operand, bit-wise AND volition provide 0 solely if both operand don't cause got a develop flake (1) at same location. In other words, if kickoff operand has 0 at LSB thus bit operand must cause got 1 or vice-versa, but they must non cause got 1 at same position. Now let's come upwards dorsum to binary subtraction,  If yous produce binary subtraction yesteryear hand, yous volition realize that it at-least modify your LSB from 0 to 1, in addition to tin switch the flake until it run across a 1 equally shown below

  1000
- 0001
-------
  0111

So inwards lodge for a number to survive a ability of ii it must follow a designing where if number = abcd1000 thus n-1 = abcd0111 in addition to abcd must survive zero.
 In this Java Programming tutorial yous volition larn how to cheque if number is Power of ii u How to exercise bitwise Operator inwards Java? Power of ii example

Since whatever binary number, which is ability of ii has precisely 1 develop bit, in addition to subtracting 1 from that volition brand all lower bits 1, (number & (number-1) will ever survive nil for number which is ability of two.

That's all well-nigh how to honor if a number is ability of ii inwards Java. As I said, at that spot are multiple ways to solve this problem, yous tin either exercise arithmetics operator e.g. partitioning or modulo operator or yous tin only exercise bit-wise operator, just similar I cause got used here.  If yous are likewise serious well-nigh improving your noesis on bitwise operator, thus yous should read hacker's delight, a peachy mass for programmers.

If yous similar this coding employment in addition to desire to exercise to a greater extent than to meliorate your coding skill, thus yous tin likewise search for Java programming exercises inwards this blog.

Subscribe to receive free email updates:

0 Response to "How to purpose bitwise Operator inwards Java? Power of 2 example"

Posting Komentar