Last active
January 6, 2016 08:53
-
-
Save skyem123/a6f912af97f25ea8d7e2 to your computer and use it in GitHub Desktop.
This file contains 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
public class BitStuff { | |
/** | |
* Rotates a long. | |
* | |
* @param data The long to be rotated. | |
* @param amount The amount to rotate the byte by, if greater than 0, rotate right, if less than 0, rotate left. | |
* @param dataLength The length of the data. If 0, assumes long length (65 bits). | |
* @return The rotated byte. | |
*/ | |
// TODO: optimise? | |
public static long rotate(long data, int dataLength, int amount) { | |
if (dataLength == 0) dataLength = Long.SIZE; | |
if (amount > 0) { | |
// Rotate Right | |
// Shift the whole thing right. | |
long rotated = data >>> amount; | |
// Get the chopped off bits and move them. | |
long removed = data << dataLength - amount; | |
// stick the chopped off bits back on. | |
rotated = rotated | removed; | |
return rotated; | |
} else if (amount < 0) { | |
amount = Math.abs(amount); | |
// Rotate Left | |
// Shift the whole thing left. | |
long rotated = data << amount; | |
// Get the chopped off bits and move them. | |
long removed = data >>> dataLength - amount; | |
// stick the chopped off bits back on. | |
rotated = rotated | removed; | |
return rotated; | |
} else { | |
// No Rotation | |
return data; | |
} | |
} | |
/** | |
* Rotates an integer. | |
* | |
* @param data The integer to be rotated. | |
* @param amount The amount to rotate the byte by, if greater than 0, rotate right, if less than 0, rotate left. | |
* @param dataLength The length of the data. If 0, assumes integer length (32 bits). | |
* @return The rotated byte. | |
*/ | |
public static int rotate(int data, int dataLength, int amount) { | |
if (dataLength == 0) dataLength = Integer.SIZE; | |
if (amount > 0) { | |
return (data >>> amount) | (data << dataLength - amount); | |
} else if (amount < 0) { | |
amount = Math.abs(amount); | |
return (data << amount) | (data >>> dataLength - amount); | |
} else { | |
// No Rotation | |
return data; | |
} | |
} | |
/** | |
* Rotates a short. | |
* | |
* @param data The integer to be rotated. | |
* @param amount The amount to rotate the byte by, if greater than 0, rotate right, if less than 0, rotate left. | |
* @param dataLength The length of the data. If 0, assumes short length (16 bits). | |
* @return The rotated byte. | |
*/ | |
public static short rotate(short data, int dataLength, int amount) { | |
if (dataLength == 0) dataLength = Short.SIZE; | |
if (amount > 0) { | |
return (short) ((data >>> amount) | (data << dataLength - amount)); | |
} else if (amount < 0) { | |
amount = Math.abs(amount); | |
return (short) ((data << amount) | (data >>> dataLength - amount)); | |
} else { | |
// No Rotation | |
return data; | |
} | |
} | |
/** | |
* Rotates a byte. | |
* | |
* @param data The integer to be rotated. | |
* @param amount The amount to rotate the byte by, if greater than 0, rotate right, if less than 0, rotate left. | |
* @param dataLength The length of the data. If 0, assumes byte length (8 bits). | |
* @return The rotated byte. | |
*/ | |
public static byte rotate(byte data, int dataLength, int amount) { | |
if (dataLength == 0) dataLength = Byte.SIZE; | |
if (amount > 0) { | |
return (byte) ((data >>> amount) | (data << dataLength - amount)); | |
} else if (amount < 0) { | |
amount = Math.abs(amount); | |
return (byte) ((data << amount) | (data >>> dataLength - amount)); | |
} else { | |
// No Rotation | |
return data; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment