博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中二进制的程序表示_Java程序检查两个数字的二进制表示形式是否为字谜
阅读量:6903 次
发布时间:2019-06-27

本文共 870 字,大约阅读时间需要 2 分钟。

如果两个数字的二进制表示形式具有相同的0'a和1's,则它们是anagram。一个例子如下:Number 1 = 3

Binary representation of Number 1 = 0011

Number 2 = 12

Binary representation of Number 2 = 1100

这两个数字是字谜。

演示此的程序如下所示-

示例public class Example {

public static void main (String[] args) {

long x = 12, y = 3;

if(Long.bitCount(x) == Long.bitCount(y))

System.out.println("Binary representations of " + x + " and " + y + " are anagrams");

else

System.out.println("Binary representations of " + x + " and " + y + " are not anagrams");

}

}

上面程序的输出如下-Binary representations of 12 and 3 are anagrams

现在让我们了解上面的程序。

定义了x和y的值。然后,对位表示中的1进行计数。如果它们相等,则x和y的二进制表示形式是字谜,否则不是。证明这一点的代码片段如下所示-long x = 12, y = 3;

if(Long.bitCount(x) == Long.bitCount(y))

System.out.println("Binary representations of " + x + " and " + y + " are anagrams");

else

System.out.println("Binary representations of " + x + " and " + y + " are not anagrams");

转载地址:http://qvodl.baihongyu.com/

你可能感兴趣的文章
Matlab自定义安装的工具箱选项
查看>>
利用bentley view将Revit模型输出为3D PDF文档
查看>>
Log4j配置详解
查看>>
nodejs 笔记
查看>>
Fastjson是一个Java语言编写的高性能功能完善的JSON库。
查看>>
【机器学习算法-python实现】决策树-Decision tree(1) 信息熵划分数据集
查看>>
最新的goldengate monitor 12.1.3已经发布
查看>>
ASP.NET防止用户多次登录的方法
查看>>
2D多边形碰撞器优化器
查看>>
webBrowser 模拟登录
查看>>
C# 采用线程重绘图形要点记录
查看>>
About Technology Research
查看>>
java + jni + mingw实例开发(基于命令行窗口模式)
查看>>
【LeetCode】7. Reverse Integer
查看>>
Struts2总结
查看>>
CentOS6.5菜鸟之旅:VIM插件NERDtree初探
查看>>
【记录】ASP.NET MVC RegisterBundles
查看>>
odex反编译dex异常 Cannot locate boot class path file /system/framework/core.odex
查看>>
【记录】AutoMapper Project To not support ResolveUsing
查看>>
IOS开发基础知识--碎片3
查看>>