Results 1 to 7 of 7

Thread: Packing vars

  1. #1
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default [SOLVED] Packing vars

    Hey,

    I'm experimenting somewhat with the packing of vars, but I ran into a problem. ;)

    The code:
    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;
    }
    (Don't ask why I made it an Applet, I have my reasons.

    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.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default

    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"?

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    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: 102
    I die a little on the inside...
    Every time I get shot.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default

    (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.

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    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.

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    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

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    :P Mental challenge, as you said and I'm just experimenting with it :P ;) not really for a reason, just wanna know how it works :)
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. vars and if sentences in XSL-FO
    By Alan in forum XML
    Replies: 1
    Last Post: 05-31-2007, 02:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •