java中二进制的程序表示_Java程序检查两个数字的二进制表示形式是否为字谜

如果两个数字的二进制表示形式具有相同的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");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值