"Written test" 28, binary in the number of 1

Source: Internet
Author: User

/** * Title: Please implement a function, enter an integer, output the number of binary representation of 1. For example, 9 is represented as a binary is 1001, and 2 bits are 1. So if you enter 9, the function outputs 2. * Time: August 28, 2015 09:51:19 * file: Numberof1.java *cutter_point */package Bishi. Offer50.y2015.m08.d28;import Org.junit.test;public class Numberof1{public static int NumberOf1 (int n) {if (n < 0) n = n; else if (n = = 0) return 0;int count = 0;while (n > 0) {//We use bit operations, generally used to binary, we use bit operations if ((n & 1) = = 0x1) {++count;} IFN = n >> 1;} Whilereturn count;} The sword refers to the offer to the standard answer, the type of int in Java 16 binary, I do not understand/** * Baidu found in the Java binary storage is the complement, then we calculate the time can not be the first to calculate the complement, and then calculate, when the test, who TM to TIME AH!!!! * So Java suggests using the above * @param n * @return */public static int NumberOf12 (int n) {int count = 0;while (n! = 0) {++count;n = (n-1) & n;//Here Java has a small problem, Java -1>>1 or -1,-1>>10 or 1, I do not know why, so still use the above solution}//whilereturn count;} public static void Test (int n, int tag) {if (tag = = 1) System.out.println (NUMBEROF1 (n)); ElseSystem.out.println (NumberOf12 (n));} @Testpublic void Test1 () {int n = 1;int N2 = 0x7fffffff;int N3 = 0x80000000;int N4 = 0xffffffff;int N5 = 0;int N6 = -1;test (N, 1), Test (N2, 1), Test (N3, 1), Test (N4, 1), Test (N5, 1), Test (N6, 1); @Testpublic void Test2 () {int n = 1;int N2 = 0x7fffffff;int N3 = 0x80000000;int N4 = 0xffffffff;int N5 = 0;int N6 = -1;test (n, 2); Test (N2, 2), Test (N3, 2), Test (N4, 2), Test (N5, 2), Test (N6, 2); public static void Main (string[] args) {int n =-1; System.out.println (0x80000001&0x80000010);//( -1&AMP;-2) The result is:-2147483648, I am also drunk System.out.println (0x80000000 ///This TM is not 0, -2147483648system.out.println (0x80000001);//This TM is not-1, -2147483647system.out.println (0x80000010); /This TM is not-2, -2147483632system.out.println (0x00000000-1);//The tm,-1//-1 's complement, that is, the sign bit or 1system.out.println ( 0xFFFFFFFF);//This is-1}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Written test" 28, binary in the number of 1

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.