How do I know if a number is a multiplication of 2? How can we determine whether a number is an odd number? JAVA Implementation

Source: Internet
Author: User

(1) method to determine whether a number is the multiplication of 2: For the integer num, if (Num & (num-1) = 0, then this number is the multiplication of 2.

The procedure is as follows:

Public class test {
Public static void main (string [] ARGs ){
System. Out. println (issqure (512); // equivalent to test. issqure ()
}
Public static Boolean issqure (INT num) {// This method must use the static keyword, because we call this method directly in the main method, instead of calling through the class object, only static methods belong to the class itself, and can be called directly through the class without using the object
If (Num & (num-1) = 0 ){
Return true;
}
Else
Return false;
}
}

(2) determine whether a number is an odd number. If (Num % 2) = 0, the number is an odd number.

The procedure is as follows:
Public class test {
Public static void main (string [] ARGs ){
System. Out. println (test. isodd (513 ));
}
Public static Boolean isodd (INT num ){
If (Num % 2) = 0 ){
Return false;
}
Else
Return true;
}
}

 

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.