Results 1 to 7 of 7
Thread: Packing vars
- 09-18-2008, 09:03 PM #1
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
[SOLVED] Packing vars
Hey,
I'm experimenting somewhat with the packing of vars, but I ran into a problem. ;)
The code:
(Don't ask why I made it an Applet, I have my reasons.Java Code:import java.applet.Applet; public class bitwiseOperationsTest extends Applet { public void init() { new bitwiseOperationsTest(); } public bitwiseOperationsTest() { test(); } private final void test() { echo("value 1: "+val1); echo("value 2: "+val2); echo("value 3: "+val3); //packed_data = (short)(((packed_data & ~(0x7f << 16) | val1 << 16) & ~(0x7f << 8) | val2 << 8) & ~0x7f | val3); /** The above is the below in one line ;) **/ //packed_data = (short)(packed_data & ~(0x7f << 16) | val1 << 16); //packed_data = (short)(packed_data & ~(0x7f << 8) | val2 << 8); //packed_data = (short)(packed_data & ~0x7f | val3); echo("packed: "+packed_data); echo("new val 1: "+((packed_data >>> 16) & 0x7f)); // ALWAYS 0 echo("new val 2: "+((packed_data >>> 8) & 0x7f)); echo("new val 3: "+(packed_data & 0x7f)); } private final void echo(String s) { System.out.println(s); } private byte val1 = 45; private byte val2 = 95; private byte val3 = 102; private short packed_data = 0; }
The problem is that the 'new' value 1 always is 0. value 2 and 3 are fine, but 1 always is 0. How come? Please help me.Last edited by Supamagier; 09-18-2008 at 09:59 PM.
I die a little on the inside...
Every time I get shot.
- 09-18-2008, 09:09 PM #2
Try debugging your code by breaking multi step statements into single operations and printing out their values using println();
Can you post the output from your program? The actual output is often better than someone's description in text.
What is "packing of vars"?
- 09-18-2008, 09:12 PM #3
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
the Packing of vars is putting multiple variables in one. Using bitwise operations you can, for example store a few bytes/shorts in an short/int. This saves space and is more efficient code.
My output:
value 1: 45
value 2: 95
value 3: 102
packed: 24422
new val 1: 0
new val 2: 95
new val 3: 102I die a little on the inside...
Every time I get shot.
- 09-18-2008, 09:48 PM #4
(packed_data >>> 16)
Do the above with shift values from 1 to 16 and see what you get.
A few comments in you code describing how you are using the bits of the short to hold data might help you and others understand what is happening
BTW I programmed mainframe assembly code for years.Last edited by Norm; 09-18-2008 at 09:51 PM.
- 09-18-2008, 09:59 PM #5
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Thanks, but I found the problem (with some help). :)
I used << 16, while a short only has 16 bits, so I would just 'throw away' the var.I die a little on the inside...
Every time I get shot.
- 09-19-2008, 06:39 PM #6
Why (other than as a mental challenge) are you doing this?
Saving a couple of bytes, at the cost of code clarity, is a questionable choice when a terabyte disk drive costs $150
- 09-20-2008, 12:26 AM #7
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Similar Threads
-
vars and if sentences in XSL-FO
By Alan in forum XMLReplies: 1Last Post: 05-31-2007, 02:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks