给你一个10进制的数将其变成2进制后会有多少个1 ?
/*
* i.e 很优雅(GraceFul)
* binary(hex) b = 10101100
* b - 1 = 10101011
* c = 1
* b=(b&(b - 1)) = 10101000
* b - 1 = 10100111
* c = 2
* b=(b&(b - 1)) = 10100000
* b - 1 = 10011111
* c = 3
* b=(b&(b - 1)) = 10000000
* b - 1 = 01111111
* c = 4
* b = 00000000
* break
*/
public int countOneGraceful(int hex) {
int c = 0;
for (; hex != 0; c ++)
hex &= hex - 1;
return c;
}
/*
* 用于与优雅对比的函数
*/
public int countOne(int hex) {
int c = 0;
for(; (hex/2) != 0; hex/=2)
if ((hex%2) == 1)
c ++;
if ((hex%2) == 1)
c ++;
return c;
}
/*
* 测试
*/
public static void main(String[] args) {
Fibonacci f = new Fibonacci(); //我的类
System.out.println("127 to binary: " + Integer.toBinaryString(127));
System.out.println(f.countOneGraceful(127));
System.out.println("128 to binary: " + Integer.toBinaryString(128));
System.out.println(f.countOneGraceful(128));
}
分享到:
相关推荐
2进制转换为10、8、16进制 Change binary number into hexadecimal number Change binary number into decimal number. Change binary number into octal number.
Simple Binary Number Convert App in Python Free Source Code.zip
A binary search algorithm (or binary chop) is a technique for finding a particular value in a sorted list. It makes progressively better guesses, and closes in on the sought value, by comparing an ...
http://blog.csdn.net/xkzju2010/article/details/46399155
discovRE: Efficient Cross-Architecture Identification of Bugs in Binary Code是发表在NDSS'16会议上的论文。本文在SP'15的基础上提出了在效率和效果上都有提高的跨架构二进制代码漏洞检测方案。 Abstract & ...
converting floating number to binary
A New Two-Scan Algorithm for Labeling Connected Components in Binary Images,新的标记算法
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the ...
Converts Decimals in to binary and binary into decimals
发表在顶会S&P'15上的论文Cross-Architecture Bug Search in Binary Executables。本文提出一个在跨架构前提下的指定Bug代码搜索方法,通过生成符号表达式和基本块签名的方式,解决了之前使用约束求解器求解导致的...
Construction method for binary matrix with maximum branch number
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
wgetwin-1_5_3_1-binary,包含可用实例,已经实际测试过
Abstract—Finding and exploiting vulnerabilities in binary code is a challenging task. The lack of high-level, semantically rich information about data structures and control constructs makes the ...
Count the number of bits set in an unsigned number Chapter 9. Add two numbers without using arithmetic operators Chapter 10. Given an array of integers where all the numbers are appearing twice find ...
In computer science, a red–black tree is a specialised binary search tree data structure noted for fast storage and retrieval of ordered information, and a guarantee that operations will complete ...
条码识别论文 Bar Code Localization In Wavelet Domain By Using Binary Morphology Ru
2021 - A Survey of Binary Code Similarity.pdf
Binary division algorithm and implementation in VHDL
this code writen for convert convert string number to binary .