Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-18-2008, 11:03 PM
Senior Member
 
Join Date: Aug 2008
Posts: 186
Supamagier is on a distinguished road
[SOLVED] Packing vars
Hey,

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

The code:
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.
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by Supamagier : 09-18-2008 at 11:59 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-18-2008, 11:09 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
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"?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-18-2008, 11:12 PM
Senior Member
 
Join Date: Aug 2008
Posts: 186
Supamagier is on a distinguished road
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:
Quote:
value 1: 45
value 2: 95
value 3: 102

packed: 24422

new val 1: 0
new val 2: 95
new val 3: 102
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-18-2008, 11:48 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
(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 11:51 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-18-2008, 11:59 PM
Senior Member
 
Join Date: Aug 2008
Posts: 186
Supamagier is on a distinguished road
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.
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-19-2008, 08:39 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-20-2008, 02:26 AM
Senior Member
 
Join Date: Aug 2008
Posts: 186
Supamagier is on a distinguished road
: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
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
vars and if sentences in XSL-FO Alan XML 1 05-31-2007 04:24 PM


All times are GMT +3. The time now is 09:32 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org