给你一个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.
在"Search in a Binary Search Tree"这个主题中,我们主要探讨如何在二叉搜索树中高效地进行查找操作。查找操作的目标是找到树中与给定值相匹配的节点。由于二叉搜索树的特性,我们可以采用分治策略来实现快速查找:...
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 ...
java java_leetcode题解之Closest Leaf in a Binary Tree.java
discovRE: Efficient Cross-Architecture Identification of Bugs in Binary Code是发表在NDSS'16会议上的论文。本文在SP'15的基础上提出了在效率和效果上都有提高的跨架构二进制代码漏洞检测方案。 Abstract & ...
c++ C++_leetcode题解之671. Second Minimum Node In a Binary Tree.cpp
标题中的"num2bin_function.rar_binary number_floating"暗示了我们主要探讨的是关于将浮点数转换成二进制表示的计算机科学主题。在计算机系统中,数字通常以二进制(0和1的组合)形式存储和处理,包括整数和浮点数...
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 ...
发表在顶会S&P'15上的论文Cross-Architecture Bug Search in Binary Executables。本文提出一个在跨架构前提下的指定Bug代码搜索方法,通过生成符号表达式和基本块签名的方式,解决了之前使用约束求解器求解导致的...
wgetwin-1_5_3_1-binary是一个针对Windows系统的wget工具的版本,它是一个用于非交互式下载的命令行工具。wget是GNU系统下的一个网络数据获取程序,广泛应用于Linux、Unix以及Windows等操作系统中。它允许用户从...
c++ C++_leetcode题解之762. Prime Number of Set Bits in Binary
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
1. 导入项目文件:将"count_binary_0"文件导入相应的软件环境中。 2. 代码编译:检查语法错误并综合代码,将其转换为门级逻辑。 3. 仿真:使用波形模拟器观察计数器在不同输入条件下的行为。 4. 布局布线:根据目标...
java java_leetcode题解之Find Elements in a Contaminated Binary Tree.java
标题中的"Converts Decimals in to binary and binary into decimals"是指一个程序或工具,它能够实现十进制(Decimal)与二进制(Binary)之间的转换。在计算机科学和信息技术领域,这种转换是非常基础且重要的概念...
关于标题“binary matrix with maximum branch number”和描述“Construction method for binary matrix with maximum branch number”的知识点,主要涉及到二进制矩阵及其分支数概念以及构造具有最大分支数的二进制...
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 ...
本文将详细探讨如何使用Verilog来计算二进制数中1的数量,对应于标题“Counting-Number-of-1.rar_The Count”所提及的任务。 在Verilog中,计数1的数量通常涉及对二进制输入进行逐位分析。这种计数方法可以应用于...
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 ...
"binarycount.rar_Synchronous Counter_VHDL 计数器_binary counter_bina"这个标题明确指出了我们要探讨的是一个使用VHDL实现的同步二进制计数器。同步计数器的特点是其所有触发器在同一时钟边沿同步改变状态,这...