Java simple judgment odd even method

Source: Internet
Author: User

Find a way to improve your code performance today, just to see this article http://www.cnblogs.com/chinafine/articles/1787118.html

Referred to.

1. Parity judgment
Do not use I 2 = = To determine whether it is odd, because I is negative odd when not set, please use I 2! = zero to determine whether it is odd, or use the efficient formula (I & 1)! = Zero to judge.

Think today also use%2 to judge, although the value of the incoming will not appear negative, but will not fall in the pit, so pay attention to the high-efficiency formula. A quick test.

(I & 1)

Two as long as one is an even number equals 0.

Two of them are odd equals 1.

Then we find that the bit operator is basically no longer used with (&), non (~), or (|), XOR (^)

Now only use (| |) (&&)

http://blog.csdn.net/vebasan/article/details/6193916

Look at this, are all turn 2 and then the operation. Measured, found too troublesome, or do not take this to install force good, in case they also forget the trouble.

System.out.println (13&17);//1system.out.println (12&17);//0system.out.println (17&12);// 0system.out.println (12&18);//0system.out.println ( -12&18);//16system.out.println ( -12&-18);// 28system.out.println (12&-18);//12system.out.println ( -3&-7);//-7system.out.println (4|1);// 5system.out.println (13|1);//13system.out.println (13|17);//29system.out.println (12|17);//29System.out.println ( 17|12);//29system.out.println (12|18);//30system.out.println (129&128);//128system.out.println (129|128);// 129



The article also said that the use of displacement operation (>>) (<<), hehe, at that time also listen to, so troublesome things, loading force effect is not high

use shift operation instead of ' A/b ' Operation 

"/" is a very "expensive" operation, and using a shift operation will be quicker and more efficient.

Example:

public class sdiv {    public static final int num  = 16;    public void calculate (Int a)  {         int div = a / 4;             // should be replaced with  "a >>  2 ".        int div2 = a / 8;          // should be replaced with  "a >>  3 ".        int temp = a / 3;     }} 



correction:  

public class Sdiv {public static final int num = 16;          public void Calculate (int a) {int div = a >> 2;        int div2 = a >> 3;       int temp = A/3; cannot be converted to displacement operation}}



10. Replace ' A * b ' with shift operation 

Ibid.
[i] but I personally think that unless it is within a very large cycle, performance is very important, and you know exactly what you are doing before you can use this method. Otherwise, it would be uneconomical to reduce the late reading of the program by improving performance.

Example:

public class smul {    public void calculate (Int a)  {         int mul = a * 4;             // should be replaced with  "A  << 2 ".         int mul2 = 8 *  a;         // should be replaced with   "A << 3".         int temp = a  * 3;    }} 



correction:  

Package Opt;public class Smul {public void calculate (int a) {int mul = a << 2;        int mul2 = a << 3;       int temp = a * 3; Cannot convert}}


This article is from the "Fly to learn Android" blog, please be sure to keep this source http://qq445493481.blog.51cto.com/9545543/1658144

Java simple judgment odd even method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.