Last active
August 29, 2015 14:14
-
-
Save jeremyheiler/9b4200790b3bd51052b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.ByteBuffer; | |
public class U { | |
public static void main(String[] args) { | |
int i = Integer.MAX_VALUE; | |
int xint = 1419258235; | |
int yint = 1411392235; | |
System.out.println("let x = " + xint); | |
System.out.println("let y = " + yint); | |
System.out.println(); | |
ByteBuffer xbuf = wrap(xint); | |
ByteBuffer ybuf = wrap(yint); | |
System.out.println("java thinks the integer x is greater than the integer y"); | |
System.out.println(" Integer.compare(x, y) => " + Integer.compare(xint, yint)); | |
System.out.println(); | |
System.out.println("cass thinks a byte buffer of x is less than a byte buffer of y"); | |
System.out.println(" xbuf.compareTo(ybuf) => " + xbuf.compareTo(ybuf)); | |
System.out.println(); | |
byte x0 = xbuf.get(0); | |
byte x1 = xbuf.get(1); | |
byte x2 = xbuf.get(2); | |
byte x3 = xbuf.get(3); | |
byte y0 = ybuf.get(0); | |
byte y1 = ybuf.get(1); | |
byte y2 = ybuf.get(2); | |
byte y3 = ybuf.get(3); | |
System.out.println("lets compare each individual byte"); | |
System.out.println(); | |
System.out.println("unsigned comparison using Integer.compare(x, y)"); | |
System.out.format("%4s %4s\n", "x", "y"); | |
System.out.println("----------------"); | |
System.out.format("%4d %4d %4d\n", unsigned(x0), unsigned(y0), Integer.compare(unsigned(x0), unsigned(y0))); | |
System.out.format("%4d %4d %4d <--\n", unsigned(x1), unsigned(y1), Integer.compare(unsigned(x1), unsigned(y1))); | |
System.out.format("%4d %4d %4d\n", unsigned(x2), unsigned(y2), Integer.compare(unsigned(x2), unsigned(y2))); | |
System.out.format("%4d %4d %4d\n", unsigned(x3), unsigned(y3), Integer.compare(unsigned(x3), unsigned(y3))); | |
System.out.println(); | |
System.out.println("signed comparison using Byte.compare(x, y)"); | |
System.out.format("%4s %4s\n", "x", "y"); | |
System.out.println("----------------"); | |
System.out.format("%4d %4d %4d\n", x0, y0, Byte.compare(x0, y0)); | |
System.out.format("%4d %4d %4d <--\n", x1, y1, Byte.compare(x1, y1)); | |
System.out.format("%4d %4d %4d\n", x2, y2, Byte.compare(x2, y2)); | |
System.out.format("%4d %4d %4d\n", x3, y3, Byte.compare(x3, y3)); | |
System.out.println(); | |
System.out.println("notice anything similar?"); | |
// System.out.println("--------"); | |
// System.out.println("xint = " + xint + "\nxint + " + fromBuffer(xbuf)); | |
// System.out.println("--------"); | |
// System.out.println("yint = " + yint + "\nyint + " + fromBuffer(ybuf)); | |
// System.out.println("--------"); | |
// System.out.println(unsigned((byte) -128)); | |
// System.out.println(unsigned((byte) 127)); | |
// System.out.println(unsigned((byte) 1)); | |
// System.out.println(unsigned((byte) -1)); | |
} | |
public static ByteBuffer wrap(int i) { | |
byte a = (byte)((i & 0x000000FF)); | |
byte b = (byte)((i & 0x0000FF00) >> 8); | |
byte c = (byte)((i & 0x00FF0000) >> 16); | |
byte d = (byte)((i & 0xFF000000) >> 24); | |
// System.out.println(d); | |
// System.out.println(c); | |
// System.out.println(b); | |
// System.out.println(a); | |
return ByteBuffer.wrap(new byte[]{d, c, b, a}); | |
} | |
public static int unsigned(byte i) { | |
return (i < 0) ? ((int) i) & 0x000000FF : i; | |
} | |
public static int fromBuffer(ByteBuffer buf) { | |
return ((unsigned(buf.get(0)) << 24) | |
+ (unsigned(buf.get(1)) << 16) | |
+ (unsigned(buf.get(2)) << 8) | |
+ (unsigned(buf.get(3)))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment