打印出Java基本类型(boolean,byte,char,string,long,int,double)值的二进制位。

虽然Java本身就提供了一些这样的方法,如Integer.toBinaryString(),不过,自定义的方式在显示上更加完整。同时也是对Java位操作的一个学习了。

以下代码为Groovy脚本

 

def MASK1 = 0X01

def buff = new ByteArrayOutputStream()
def out = new DataOutputStream(buff)

out.writeInt(Integer.MAX_VALUE)

byte[] byteArr = buff.toByteArray()

def strBuff = new StringBuffer()
for(i in 0..<byteArr.length){
    for(j in 0..<Byte.SIZE)
        strBuff.append((byteArr[i] >> (Byte.SIZE - j - 1)) & MASK1)
        
    strBuff.append(' ')
}

println strBuff.toString() 

相关文章:

  • 2022-12-23
  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
  • 2021-08-25
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-06
  • 2021-09-20
  • 2021-12-11
  • 2021-11-27
  • 2021-12-03
  • 2021-06-28
相关资源
相似解决方案