I am currently working on a project where I work bitwise. I need to get the single bits which make up a character.
Generally it would be nice if Processing offered a way to
work with single bits within multiple bits (bitsets) like nibbles, bytes, etc... depending on what datatypes you are working with (characters, colors, arbitrary BitSets,...). Or to tell me if something like this already exists within Processing (or the underlying Java) and how to work with it.
I would like that objects of nearly all types (where technically possible) have a method like
object.bitAt(int bitPointer, String bitEndianness) which returns the flag (=boolean) of the bit at the position where
bitPointer points to, where the optional argument
bitEndianness determines in which direction the object's bits are being read. A similar method should be available for writing.
My (resource inefficient) workaround code meanwhile, note function
bitAt():
Quote:// Basic Setup
void setup() {
size(100,100);
noLoop();
}
// Draw Setup
void draw() {
charDump(str(char1),'u'); // UnicodeChar is dumped to console as 16 bit. Strangely the more significant bits are written on the left side through the binary() function. But in western cultures we read from left-to-right and count from smaller to larger. Hence I would expect that the least significant bit is written on the left (=the beginning) of the string. Processing (or is it the underlying Java?) has a inconsistency here!!!
bitAtLoop(char1, "up", 0, 15); // The "up" mode matches the outcome of the binary() function.
println("---");
charDump(str(char1),'u');
bitAtLoop(char1, "down", 0, 15); // The "down" mode doesn't match the outcome of the binary() function.
println("---");
charDump(str(char1),'b'); // UnicodeChar is first cut to 8 bit. Then dumped to console.
bitAtLoop(char1, "up", 8, 15); // The mode "up" and the limiters from 8 to 15 math the outcome of the binary() function.
}
// Variable Setup
String str1 = "Some Characters";
char char1 = 'X';
int index1 = 7;
// Function Setup
boolean bitAt(char c, int pointer) {
String b = binary(c);
if (b.charAt(pointer) == '1') {
return true;
}
else if (b.charAt(pointer) == '0') {
return false;
}
else {
return false;
}
}
void bitAtLoop(char c, String direction, int low, int high) {
if (direction == "up") {
for (int i = low; i <= high; i++) {
println(bitAt(c , i));
}
}
if (direction == "down") {
for (int i = high; i >= low; i--) {
println(bitAt(c , i));
}
}
}
void charDump(String s, char mode) {
switch (mode) {
case 'b': // Byte 8-Bit
for (int i = 0 ; i <= s.length()-1 ; i++ ) {
println(s.charAt(i) + " " + binary(byte(s.charAt(i))) + " 0x" + hex(byte(s.charAt(i))));
}
break;
case 'u': // Unicode 16-Bit
for (int i = 0 ; i <= s.length()-1 ; i++ ) {
println(s.charAt(i) + " " + binary(s.charAt(i)) + " 0x" + hex(s.charAt(i)));
}
break;
}
}