We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › int masked to 4 bytes
Page Index Toggle Pages: 1
int masked to 4 bytes (Read 1394 times)
int masked to 4 bytes
May 5th, 2005, 10:23pm
 
I'v figured out I can pack chunks of information in my program into 8 bytes (a snack if you will). Four bytes of that is going to be a counter as big as an int can go (Integer.MAX_VALUE - it's about 2 American billion either way of zero).

I've gotten as far as figuring out the masking for the first byte and I'm stuck on the rest. I was thinking of using either side of zero but I'm really stuck with the math here. I guess I could work with a byte's upper limit of 127 and stay in the positive but that gives me a maximum number of 268,435,456. Plus I've got to work out how to decode the 4 byte array to get the information out of the file I'm going to save from this.
Code:

byte [] counter;
int number;
void setup(){
counter = new byte[4];
}
void draw(){
number++;
byteCounter(number);
String output;
for (int i = 0; i < counter.length; i++){
output = ouptut + " " + counter[i];
}
println(number+output)
}
void byteCounter (int var){
counter[0] = var&0xFF-128;
counter[1] = var&0x //err...
}

Can anyone help?

-I've done a search on this and it feels like I'm asking the impossible. I can't even work out how to convert ints into bytes let alone deal with Java's lack of unsigned number support.
Re: int masked to 4 bytes
Reply #1 - May 5th, 2005, 11:16pm
 
assumign you have a 4 byte int source:

Code:

int byte1=(source&0xFF000000)>>24;
int byte2=(source&0x00FF0000)>>16;
int byte3=(source&0x0000FF00)>>8;
int byte4=(source&0x000000FF);

">>" is the bit-shift-right operator, and so >>24 moves all the bits 24 places to the right so 0x12345678 & 0xFF000000 becomes 0x12000000, shift that right 24 bits, you end up with 0x00000012
Re: int masked to 4 bytes
Reply #2 - May 6th, 2005, 2:39am
 
Thankyou. I'm almost there but the extrapolation isn't working. Obviously I've got the return value wrong (if any of them is negative or zero it cocks up). Is there a speedy solution to this? And could my operation be optimised in anyway?
Code:

byte [] counter;
int number;
void setup(){
counter = new byte[4];
}
void draw(){
number++;
//write bytes to file
if (number < 512){
byteCounter(number);
String output = "";
for (int i = 0; i < counter.length; i++){
output = output + " " + counter[i];
}
println(number+output);
}
if (number == 511){
saveBytes("counting.dat",counter);
}
}

void mousePressed(){
//extrapolate numbers from file
byte [] bytesLoaded;
bytesLoaded = loadBytes("counting.dat");
for (int i = 0; i < bytesLoaded.length; i+=4){
byte [] sendBytes = new byte[4];
System.arraycopy(bytesLoaded, i, sendBytes, 0, 4);
println(extrapolate(sendBytes));
//println (bytesLoaded[i]);
}
}

void byteCounter (int source){
counter[0]=byte((source&0xFF000000)>>24);
counter[1]=byte((source&0x00FF0000)>>16);
counter[2]=byte((source&0x0000FF00)>>8);
counter[3]=byte((source&0x000000FF));
}
int extrapolate (byte [] source){
int [] sendInt = new int[source.length];
for (int i = 0; i < source.length; i++){
sendInt[i] = int(source[i]);
}
return sendInt[0] * sendInt[1] * sendInt[2] * sendInt[3];
}
Re: int masked to 4 bytes
Reply #3 - May 6th, 2005, 3:00am
 
Is this what you're trying to do?

Code:
return (sendInt[0] << 24)
| (sendInt[1] << 16)
| (sendInt[2] << 8)
| sendInt[3];
Re: int masked to 4 bytes
Reply #4 - May 6th, 2005, 3:47am
 
Code:

byte [] counter;
int number;
void setup(){
counter = new byte[16384];
}
void draw(){
number++;
//write bytes to file
if (number < 4096){
byteCounter(number);
String output = "";
for (int i = 0; i < 4; i++){
output = output + " " + counter[(number<<2)+i];
}
println(number+output);
}
if (number == 4095){
saveBytes("counting.dat",counter);
println("a");
}
}

void mousePressed(){
//extrapolate numbers from file
byte [] bytesLoaded;
bytesLoaded = loadBytes("counting.dat");
for (int i = 0; i < bytesLoaded.length; i+=4){
byte [] sendBytes = new byte[4];
System.arraycopy(bytesLoaded, i, sendBytes, 0, 4);
println(extrapolate(sendBytes));
println (bytesLoaded[i+3]);
}
}

void byteCounter (int source){
counter[source<<2]=byte((source&0xFF000000)>>24);
counter[(source<<2)+1]=byte((source&0x00FF0000)>>16);
counter[(source<<2)+2]=byte((source&0x0000FF00)>>8);
counter[(source<<2)+3]=byte((source&0x000000FF));
}
int extrapolate (byte [] source){
int [] sendInt = new int[source.length];
for (int i = 0; i < source.length; i++){
sendInt[i] = int(source[i]);
}
return (sendInt[0] << 24)
| (sendInt[1] << 16)
| (sendInt[2] << 8)
| sendInt[3];
}

Nearly, the return sends positive every second integer. It's that old signed number problem of java. Nearly there. I'll have cut my project data chunks down to half the size if this works (down from 300k by saveStrings to 160k from saveBytes - using some lookup tables when the program sets up instead of calculating on the fly).
Re: int masked to 4 bytes
Reply #5 - May 6th, 2005, 12:21pm
 
my bad, I forgot to reminder

println (bytesLoaded[i+3]);

in the print out. V.sorry.

Thankyou v.much for the help though.
Page Index Toggle Pages: 1