一共2个文件:
PrintBinary.java --- 打印整形的二进制格式
PrintBinaryTest.java --- 测试文件
主要内容:
打印整型的二进制格式
主要功能:
1. void printBinaryNumber(int x)
打印整型的二进制格式
文件1:PrintBinary.java
package Common.Utils.DataUtils;
/**
*
* <p>
* Description: 打印整型的二进制格式
* </p>
* <p>
* Copyright 2006 mjn.
* </p>
*
* @author ma jia nan
* @Create Date : 2008-02-24
* @Version : 1.0
*/
public class PrintBinary {
/**
* 打印整型的二进制格式
*
* @param x
* @return
*/
public void printBinaryNumber(int x) {
int[] buffer = new int[32];
for (int i = 0, mask = 1; i < 32; i++) {
mask = 1;
mask = mask << i;
buffer[i] = (mask & x) >> i;
}
for (int j = 31; j >= 0; j--) {
System.out.print(buffer[j]);
if (j % 8 == 0)
System.out.print(" ");
}
System.out.println("");
}
}
文件2:PrintBinaryTest.java /**
*
* <p>
* Description: 打印整型的二进制格式
* </p>
* <p>
* Copyright 2006 mjn.
* </p>
*
* @author ma jia nan
* @Create Date : 2008-02-24
* @Version : 1.0
*/
public class PrintBinary {
/**
* 打印整型的二进制格式
*
* @param x
* @return
*/
public void printBinaryNumber(int x) {
int[] buffer = new int[32];
for (int i = 0, mask = 1; i < 32; i++) {
mask = 1;
mask = mask << i;
buffer[i] = (mask & x) >> i;
}
for (int j = 31; j >= 0; j--) {
System.out.print(buffer[j]);
if (j % 8 == 0)
System.out.print(" ");
}
System.out.println("");
}
}
import Common.Utils.DataUtils.PrintBinary;
/**
*
* <p>
* Description: 测试PrintBinary
* </p>
* <p>
* Copyright 2006 mjn.
* </p>
*
* @author ma jia nan
* @Create Date : 2008-03-24
* @Version : 1.0
*/
public class PrintBinaryTest {
public static void main(String args[]) {
System.out.println(s((byte) 100));
System.out.println(s((byte) 1));
System.out.println(s((byte) -1));
}
private static byte s(byte b) {
return new test().swap(b);
}
private void p(int x) {
new PrintBinary().printBinaryNumber(x);
}
/**
* 交换b二进制形式(8位)的前4位和后4位
*
* @param b
* @return
*/
public byte swap(byte b) {
int lowBits = b & 0xF;
int highBits = b & 0xF0;
int result = lowBits << 4 | highBits >> 4;
System.out.println("\n\nb=" + b + " 0xF=" + 0xF + " 0xF0=" + 0xF0);
System.out.print(" b Binary : ");
p(b);
System.out.print(" 0xF Binary : ");
p(0xF);
System.out.print(" lowBits(b&0xF) : ");
p((b & 0xF));
System.out.print("\n b Binary : ");
p(b);
System.out.print(" 0xF0 Binary : ");
p(0xF0);
System.out.print("highBits(b&0xF0): ");
p((b & 0xF0));
System.out.print("\n lowBits << 4 : ");
p((lowBits << 4));
System.out.print(" highBits >> 4 : ");
p((highBits >>> 4));
System.out.print(" result( | ) : ");
p((result));
return (byte) result;
}
}
本文转自BlogJavaOo缘来是你oO的博客,原文链接:打印整型的二进制格式,如需转载请自行联系原博主。/**
*
* <p>
* Description: 测试PrintBinary
* </p>
* <p>
* Copyright 2006 mjn.
* </p>
*
* @author ma jia nan
* @Create Date : 2008-03-24
* @Version : 1.0
*/
public class PrintBinaryTest {
public static void main(String args[]) {
System.out.println(s((byte) 100));
System.out.println(s((byte) 1));
System.out.println(s((byte) -1));
}
private static byte s(byte b) {
return new test().swap(b);
}
private void p(int x) {
new PrintBinary().printBinaryNumber(x);
}
/**
* 交换b二进制形式(8位)的前4位和后4位
*
* @param b
* @return
*/
public byte swap(byte b) {
int lowBits = b & 0xF;
int highBits = b & 0xF0;
int result = lowBits << 4 | highBits >> 4;
System.out.println("\n\nb=" + b + " 0xF=" + 0xF + " 0xF0=" + 0xF0);
System.out.print(" b Binary : ");
p(b);
System.out.print(" 0xF Binary : ");
p(0xF);
System.out.print(" lowBits(b&0xF) : ");
p((b & 0xF));
System.out.print("\n b Binary : ");
p(b);
System.out.print(" 0xF0 Binary : ");
p(0xF0);
System.out.print("highBits(b&0xF0): ");
p((b & 0xF0));
System.out.print("\n lowBits << 4 : ");
p((lowBits << 4));
System.out.print(" highBits >> 4 : ");
p((highBits >>> 4));
System.out.print(" result( | ) : ");
p((result));
return (byte) result;
}
}