java中关于二进制的初步。
两个int型和一个long型的转换:
long now=1368257088802L;
int low = (int) (0xFFFFFFFFL & now);
int heigh = (int) ((0xFFFFFFFF00000000L & now) >> 32);
System.out.println("long转双int:"+heigh+","+low);
long lowL=low;
long heighL=heigh;
long now2= ((long)lowL & 0xFFFFFFFFl) + (((long)heighL << 32) & 0xFFFFFFFF00000000l);
System.out.println("双int转long:"+now2);
System.out.println("now:"+now);
ByteBuffer buffer= ByteBuffer.allocate(100);
buffer.putLong(now);
buffer.flip();
long three=buffer.getInt();
long four=buffer.getInt();
System.out.println(three+","+four);
long now3=(((three<<32)&0xFFFFFFFF00000000l)+(four& 0xFFFFFFFFl));
System.out.println("高位+低位:"+now3);
Byte aa='\n';
System.out.println(aa);
int value=61156;
ByteBuffer byte_buffer= ByteBuffer.allocate(4);
byte_buffer.putInt(value);
byte_buffer.flip();
byte aa1=byte_buffer.get();
byte aa2=byte_buffer.get();
byte aa3=byte_buffer.get();//大于128的byte的补码为负值,小于128的byte为正值
byte aa4=byte_buffer.get();
System.out.println(aa1+","+aa2+","+aa3+","+aa4);
int test=0;
test+=( aa1<<24 &0xFF000000);
test+=(aa2<<16 &0xFF0000);
// test+=(aa3<<8 & 0xFF00);//一直是正确的
// test+=(aa3 & 0xFF)<<8;//一直是正确的
test+=( aa3 <<8 );//当为负值时,是错误的;只有为正值是才是正确的
test+=( aa4&0xFF) ;
System.out.println(test);
//转换成二进制
System.out.println( Integer.toBinaryString(aa4 ) +","+aa4 );
System.out.println( Integer.toBinaryString(aa4 & 0xFF)+","+(aa4 & 0xFF) );
System.out.println("<---------------------分割线-------------------------->");
int test_byte=129;
byte byte_test=(byte)(test_byte);
System.out.println( Integer.toBinaryString(byte_test ) +","+byte_test );
System.out.println( Integer.toBinaryString(byte_test & 0xFF)+","+(byte_test & 0xFF) );
int temp=1;
if((byte_test & temp)>0)
{
System.out.println("---");
}
System.out.println("<---------------------分割线-------------------------->");
int a_255=128;
byte test_1=(byte)(a_255);
System.out.println("a_255:"+(test_1 &0xFF) );
System.out.println("a_255:"+(test_1 ) );
个人备注:转换后的二进制的表示数值的有效位是一样的,高位是补码的填充。