Created
August 1, 2022 16:35
-
-
Save markozajc/1f96e5ff24ef9bddaa641cc086e48d8c 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
import static java.lang.Integer.toHexString; | |
import static java.lang.Math.*; | |
import static java.lang.System.out; | |
import static java.util.Arrays.stream; | |
public class Bitfuck { | |
public static void main(String[] args) { | |
var counts = new int[] { 6, 30, 13 }; | |
var names = new String[] { "firstvalue", "secondvalue", "thirdvalue"}; | |
var u = new StringBuilder(); | |
var p = new StringBuilder(); | |
int bytePos = 0; | |
int bitPos = 0; | |
int displayPos = stream(names).mapToInt(String::length).max().orElse(0) + 1; | |
for (int i = 0; i < counts.length; i++) { | |
out.print(names[i]); | |
out.print(" ".repeat(displayPos - names[i].length())); | |
u.append("int "); | |
int bitCount = counts[i]; | |
while (bitCount > 0) { | |
int bitsUsed = min(bitCount, 8); | |
if (bitsUsed > 8 - bitPos) | |
bitsUsed -= bitPos; | |
int shift = bitCount - bitsUsed; | |
if (shift == 0) | |
shift = -(8 - bitCount - bitPos); | |
u.append(names[i]); | |
u.append(" "); | |
if (bitCount != counts[i]) | |
u.append("|"); | |
u.append("= "); | |
if (bitsUsed != 8 && bitPos != 0) | |
u.append("("); | |
p.append("packed["); | |
u.append("toUnsignedInt(packed["); | |
p.append(bytePos); | |
u.append(bytePos); | |
p.append("] "); | |
u.append("])"); | |
if (bitsUsed != 8 && bitPos != 0) { | |
u.append(" & 0x"); | |
u.append(toHexString((2 << bitsUsed - 1) - 1 << max(8 - bitPos - bitsUsed, 0))); | |
u.append(")"); | |
} | |
if (bitPos != 0) | |
p.append("|"); | |
p.append("= (byte) ("); | |
if (shift != 0) | |
p.append("("); | |
p.append(names[i]); | |
p.append(" & 0x"); | |
p.append(toHexString((2 << bitsUsed - 1) - 1 << max(shift, 0))); | |
if (shift != 0) { | |
p.append(") "); | |
u.append(" "); | |
if (shift > 0) { | |
p.append(">> "); | |
u.append("<< "); | |
} else { | |
p.append("<< "); | |
u.append(">> "); | |
} | |
p.append(abs(shift)); | |
u.append(abs(shift)); | |
} | |
p.append(");\n"); | |
u.append(";\n"); | |
out.print("1".repeat(bitsUsed)); | |
displayPos += bitsUsed; | |
if (bitPos + bitsUsed >= 8) { | |
bytePos++; | |
displayPos++; | |
out.print(" "); | |
} | |
bitCount -= bitsUsed; | |
bitPos = (bitPos + bitsUsed) % 8; | |
} | |
out.println(); | |
} | |
out.println(); | |
out.println(p.toString()); | |
out.println(u.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment