Packing and Unpacking BITS from a SHORT

Hello everyone,
I am have six pieces of data packed into a 16bit short value and I am wondering if my code to pack and unpack the short looks right to everyone else. The six smaller values that I am pulling out of the short are of the length (in order) 2+2+3+3+3+3 bits . My approach to unpacking the short is applying a bitmask to isolate just the sub-value I want, then using the bitwise-left shift ( << operator) to move that value to the 0-position in the short, then casting the value to a byte for use in my program. The code to do that looks something like this:
public final short maskValue1 = Short.parseShort("0000000000000011", 2);
public final short maskValue2 = Short.parseShort("0000000000001100", 2);
public final short maskValue3 = Short.parseShort("0000000001110000", 2);
public final short maskValue4 = Short.parseShort("0000001110000000", 2);
public final short maskValue5 = Short.parseShort("0001110000000000", 2);
public final short maskValue6 = Short.parseShort("111000000000000", 2);
     public void unpackEncodedMap(short map, byte[] outMap) {
          outMap[0] = (byte)((map & maskValue1) << 14);
          outMap[1] = (byte)((map & maskValue2) << 12);
          outMap[2] = (byte)((map & maskValue3) << 9);
          outMap[3] = (byte)((map & maskValue4) << 6);
          outMap[4] = (byte)((map & maskValue5) << 3);
          outMap[5] = (byte)(map & maskValue6);
The approach I use to encode the short value involves taking my input byte array and casting each into a short. Then I use the bitwise-right shift operator ( >> ) to move the bit value into the appropriate position. To apply the next byte-value I bitwise-OR the previous value onto the new mask. The values in the byte-array are assured not to be greater then their sub-value bit lengths ... so the downcast is all right. Here is my code to do the packing:
     public short encodeMap(byte[] mymap) {
          short out = (short)(((short)mymap[0]) >> 2);
          out = (short)((out | ((short)mymap[1])) >> 2);
          out = (short)((out | ((short)mymap[2])) >> 3);
          out = (short)((out | ((short)mymap[3])) >> 3);
          out = (short)((out | ((short)mymap[4])) >> 3);
          out = (short)(out | ((short)mymap[5]));
          return out;
Thanks for looking at my functions and point out any inconsistencies. I feel like there is something I am not seeing in my code. Thanks!

Well...thanks for the one-line answer :) yes, in the sense that it compiles and runs, but I am not sure if there is a problem with my logic or not.My program is acting strange and I am trying to isolate my problem, I think the problem might be in my pack/unpack methods.... possibly in my bit-math.

Similar Messages

  • Pack and unpack in ABAP objects

    can anyone explain me how to pack or unpack a field inside a method which is inside a class.

    Hi
    See the doc of PACK and UNPACK
    PACK source TO destination.
    This statement, <b>which is forbidden in classes</b>, converts the content of the data object source to the data type p of length 16 without decimal places. In contrast to the conversion rules for elementary data types, a decimal separator in source is ignored. This assigns the converted content to the data object destination.
    The data type of source must be character-type, flat, and its content must be interpretable as a numeric value. The data type of destination must be flat. If destination has the data type p, the interim result is assigned to it from left to right. Surplus characters are cut off on the left, and the decimal places are determined by the data type of destination. If destination does not have the data type p, the interim result is converted to the data type of destination according to the rules in the conversion table for source field type p.
    UNPACK source TO destination.
    This statement converts the content of the data object source according to a specific rule and assigns the converted content to data object destination. For source, the data type p of length 16 without decimal places is expected. The data type of destination must be character-type and flat.
    The conversion is performed according to the following rules:
    If the data type of source is not of the type p with length 16 and without decimal places, then the content of source is converted to this data type. Contrary to the rules described in conversion rules for elementary data types, any decimal separator in source is completely ignored.
    The digits of the interim result are assigned to data object destination right-aligned and without +/- sign. Any additional places in destination are filled with leading zeros. If the length of destination is not sufficient, the assigned variable is truncated from the left.
    Reward points for useful Answers
    Regards
    Anji

  • WebLogic Pack and Unpack

    When you pack and unpack the domain from one server to another server, config.xml is missing while we unpack the same,
    here we would like to know, do we need to metion the same path(Same folder structure) while pack and unpack?

    Hi
    Yes, one of the basic requirements is all the Machines should have Absolutely same version of Weblogic Software and in the same folder structure. The reason is when we run Pack command on the main master domain, it does store the full folder structure of some folders that has all the modules. So when you run UnPack on this other machine, basically it will unpack. But runtime, if folder structure is not matching, the most common error you may get is like Java home not found, middleware home not found and ofcourse ends up with all NoClassDefFound errors.
    Hence the folder structure should be same. And one more thing. After running unpack command, immediately you will NOT see any managed server folders under your domain/servers. That folder will be still Empty only. Only when you start the managed server like startManagedWeblogic.cmd myMS1 adminurl, thats when under domain/servers you will see a new folder named "myMS1" which is the name of managed server that you gave when you created the main master domain and packed it. Also, note it will prompt for username and password. ALSO, everytime you start managed servers, it will go to Admin Server and get some important files like config.xml file, jdbc files and few other config files and mainly all the LDAP files. Basically everytime it Syncs from AdminServer. So do NOT make any changes to any files on ManagedServer. Because when you restart they will get overwritten by admin files.
    Thanks
    Ravi Jegga

  • Pack and unpack a JFrame

    Hi,
    I'm using a JFrame and I 'd like to put a JButton to pack and unpack the window. Now, the pack is simple (I use the pack() method), but to enlarge the window? I've tried to store the original size before pack, and then resize the window but, since I use a BorderLayout layout manager the center panel doen't return visible.
    can anyone help me?

    You likely are not calling validate() on the layout after the resize
    Eximport javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Test extends JFrame implements ActionListener
       private JPanel p;
       private Rectangle r;
       public Test()
          super("Flip");
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setSize(770,580);
          p = new JPanel(new BorderLayout());
          JPanel p2 = new JPanel(new FlowLayout());
          p2.add(new JLabel("North"));
          p.add(p2, BorderLayout.NORTH);
          p2 = new JPanel(new FlowLayout());
          p2.add(new JLabel("West"));
          p.add(p2, BorderLayout.WEST);
          p2 = new JPanel(new FlowLayout());
          p2.add(new JLabel("East"));
          p.add(p2, BorderLayout.EAST);
          p2 = new JPanel(new FlowLayout());
          p2.add(new JLabel("Center"));
          p.add(p2, BorderLayout.CENTER);
          p2 = new JPanel(new FlowLayout());
          JButton pack = new JButton("Pack");
          JButton unpack = new JButton("Unpack");
          pack.addActionListener(this);
          unpack.addActionListener(this);
          p2.add(pack);
          p2.add(unpack);
          p.add(p2, BorderLayout.SOUTH);
          setContentPane(p);
       public void actionPerformed(ActionEvent ae)
          String s = ae.getActionCommand();
          if(s.equals("Pack"))
             r= getBounds();
             pack();
          else
             setBounds(r);
             validate();
       public static void main(String[] args)
          new Test().setVisible(true);
    }

  • Pack and Unpack in HU

    Hi MM Gurus,
    I have a requirement of packing one HU of say 5EA Qty and Unpack in two HUs say 4EA & 1EA.Is this scenario is  possible in Handling Unit Management. If possible please explain.
    Regards,
    Manikyappa

    hi,
    if you have handling unit management activated (TVSHP-EXIDV_UNIQUE), you can create HUs within hu02. Here you can pack the f.e. 5 pieces into one HU or also unpack it into two HUs with 4+1 pieces (add material, total quantity, plant and storage location and the packaging material -> select these to lines and push "pack" button in the middle of the screen for packing it; to unpack HUs use the "empty" or "delete HU" button on the left side of the screen, after this you can pack again like you want... you have several possibilities to create HUs within this transaction). You can also use transaction humo for changing HUs (and for a lot of useful things used with HUs).
    (If you do not have handling unit management activated, you can only create shipping units within a delivery (you have the same screen there as in hu02).)
    I hope I could help you.
    Regards, Ely

  • Hanlding Unit Pack and Unpack Function module

    Hi All,
        Can any one know the functiona modules for PACK Handling Unit and UNPACK Handling Unit? If so please reply me.
    Thanks,
      Pranav

    Hi,
    Please check this FM.
    PACK_HANDLING_UNIT
    UNPACK_HANDLING_UNIT
    Hope this will help.
    Regards,
    Ferry Lianto

  • Changing WL port using pack and unpack Commands

    Hi,
    I am an experienced WL8 user, just starting with WL9.
    I created a first domain relatively easily using "config.sh"; and after finishing this proof of concept thought that it might be a good idea to create a template from it for the rest of the guys here.
    For that I used the ?pack? command, which works fine.
    The completing ?unpack? command also seems to works fine.
    The only thing is that I fail to locate any option to determine the server?s listen port (always sets to be 7001 like my original domain), and this is problem because we all work on the same linux machine.
    The only option I found so far is to start the server on it original port (7001) and than change it using the admin console. This is not good enough since we want to use an automatic install wizard?
    Did I miss something?
    Is there any command line to help?
    Or any other tips?
    Your help is appreciated,
    zilbi

    Hi Again,
    solved it using the WLST offline tool.
    the script (customCmpDomain.py):
    #=======================================================================================
    # Open the domain template.
    #=======================================================================================
    readTemplate("cmpDomainTemplate.jar")
    #=======================================================================================
    # Change server's port
    #=======================================================================================
    cd('Servers/AdminServer')
    set('ListenPort', 7080)
    #=======================================================================================
    # Open (unpack) the domain.
    #=======================================================================================
    writeDomain("/home/rami/domains/cmpDomain")
    #=======================================================================================
    # Close & Exit
    #=======================================================================================
    closeTemplate()
    exit()
    and just run it using 'weblogic.WLST'
    thanks anyway!
    zilbi

  • String pack and unpack

    Hi, i�m having trouble trying to write a methode to unpack a string packed with the following methode.
    can some one tell me how to write the unpack methode ?
    thanks.
    public String pack(String s) {
    byte[] a = new byte[16];
    //If the string is an odd length then pack with an F
    if ( s.length() % 2 == 1 ) {  
    s = (char) 0xFF + s ;
    //Now pack each pair of bytes into the array
    for ( int i = 0, j = 0 ; i < s.length() ; i += 2, j++ ) {
    a[j] = (byte) ( ( (s.charAt(i) & 0x0F) << 4 ) +
    (s.charAt(i+1) & 0x0F) ) ;
    return new String(A);
    }

    You need know the assumptions about the content of the original string. A packing algorithm like the one you describe might be used to pack a numeric string into an array of bytes. In that case, you assume that the original string consisted of nothing but ASCII digits, and you can recover the string with the following method:
    public String unpack(String a) {
      byte [] b = a.getBytes();
      StringBuffer buff = new StringBuffer(b.length * 2);
      for (int i = 0; i < b.length; i++) {
        buff.append((char)(0x30 | ((b[i] & 0xf0) >> 4)));
        buff.append((char)(0x30 | (b[i] & 0x0f)));
      // If the first char was a pad char, then remove it
      if (buff.charAt(0) == (char)0x3f)
        return buff.substring(1);
      return buff.toString();
    }

  • Pack and unpack

    Hi
    Please do help me.
      Data: dmshb(11),             "amount in SEK or EUR
              amount_1    LIKE dfkkop-betrw.  " of length 13 and decimal 2 with +/-sign
      amount_1 = '   91111111.00-'.
      dmshb = amount.
    the result of this is   dmshb = *111111.00-
    the expected is  dmshb = 09111111100
    Please do tell me the logic to do this
    promise to reward points
    bye
    Mac

    Hi Madan,
    This is your code.
    REPORT ZTEST_TEST01 .
    Data: dmshb(11), "amount in SEK or EUR
    amount_1 type netpr. " of length 13 and decimal 2 with +/-sign
    amount_1 = ' 91111111.00-'.
    unpack amount_1 to dmshb.
    *dmshb = amount_1.
    write: / 'amount', amount_1 ,
             'dmshhb', dmshb.
    Regards,
    Raghav

  • Pack and unpack Radius VSA attributes

    Hi
    As far as I know there are some methods to pack radius VSA attributes. Here are:
    As the part of Cisco-AVPair
    26 - VSA
    Length
    9  - Vendor ID
    1  - Vendor Type (Cisco-AVPair Attribute ID)
    Attribute Name=Value
    In the Vendor Specific attribute ("throught attribute ID")
    26  - VSALength
    9 - Vendor ID
    2 - Vendor Type (Attribute ID)
    Vendor Length
    Attribute Name=Value
    In the Vendor Specific attribute ("throught attribute ID") 
    26 - VSA
    Length
    9 - Vendor ID
    2 - Vendor Type (Attribute ID)
    Value
    i.e. with attribute name and witout.
    How to understand which attribute needs attribute name in value string?
    For example:
    26|Length|9|2|Vendor Length|1|h323-incoming-conf-id=82b5fc8cd6f411dfa3c6080027716a9a
    26|Length|9|2|Vendor Length|35|h323-incoming-conf-id=82b5fc8cd6f411dfa3c6080027716a9a
    26|Length|9|2|Vendor Length|35|82b5fc8cd6f411dfa3c6080027716a9a
    which of the methods is right?

    Hi,
    For the specific VSA you used in the example (h323-incoming-conf-id), (1) is the correct encoding, since Cisco VSA vendor type 1 (also more commonly referred to as  cisco AV Pair) is always encoded in strings with the format of "attribute=value". This applies to other cisco VSAs that use string encoding as well. For VSA's that don't use string encoding, eg., fax-pages (vendor type 5, encoding integer), it typically doesn't include the value. You should be able to check that against the vendor dictionary to confirm. Please also see:
    http://www.cisco.com/en/US/docs/ios/voice/cdr/developer/guide/cdrdefs.html
    Thanks,
    Wen

  • Can anyone help. I need to reuse my SDHC cards and transfer footage from an ongoing profect to a hard drive. Can i edit from the Hard drive or am I better transferring info striaight from camera to time line

    Hi I am using a JVC GY-HM100 camera which is meant to work well with final cut pro.  I have an ongoing project but need to save money and resuse the SDHC cards that are currently full of footage.
    Would I be better off transferring the footage straight to the time line (as rumour has it you can do this with this camera) or store the footage on a hard drive and edit it later without risk of losing the footage.
    Many thanks
    Lindabob

    Thanks Linda,
    Here's some more info for you:
    This is my pet checklist for questions regarding FCP X performance - you may have already addressed some of the items but it's worth checking.
    Check the spec of your Mac against the system requirements:
    http://www.apple.com/finalcutpro/specs/
    Check the spec of your graphics card. If it's listed here, it's not suitable:
    http://support.apple.com/kb/HT4664
    Make sure you're using the latest version of the application - FCP X 10.0.3 runs very well on my 2009 MacPro 2 x 2.26 GHz Quad-Core Intel Xeon with 16 GB RAM and ATI Radeon HD 5870 1024 MB. I run it with Lion 10.7.3.
    Check that you have at least 20% free space on your system drive.
    If you have not already done so, move your Projects and Events to a fast (Firewire 800 or faster) external HD. Make sure the drive's formatted OS Extended (journalling's not required for video). You should always keep at least 20% free space on the Hard Drives that your Media, Projects and Events are on.
    If you are getting crashes, there is some conflict on the OS. Create a new (admin) user account on your system. Do NOT import any of the settings etc from the old account - simply use FCP X from there - if it runs a lot better, there's a conflict and a clean install would be recommended - but remember, if you reinstall the system, and then use Migration Assistant (or anything else) to import all your old settings etc etc, you may well be importing the cause of the conflict in the first place.
    Keep projects to 20 mins or less (about half that for Multicam). If you have a long project, work on short sections, make them into Compound Clips and then paste these into a final project for export.
    If you ever experience dropped frames, I strongly recommend you use ProRes 422 Proxy - it edits and plays back like silk because the files are small but lightly compressed (not much packing and unpacking to do) - but remember to select 'Original or Optimised Media' (FCP X Preferences > Playback) just before you export your movie, otherwise it will be exported at low resolution.
    If you have plenty of processor power, for the ultimate editing experience, create Optimised Media - most camera native files are highly compressed and need a great deal of processor power to play back - particularly if you add titles, filters or effects. ProRes 422 takes up much more hard drive space but is very lightly compressed. It edits and plays back superbly.
    Hide Audio Waveforms at all times when you don't need them (both in Browser and Storyline / Timeline). They take up a lot of processor power. (Use the switch icon at the bottom-right of your timeline to select a format without waveforms if you don't need them at the moment, then switch back when you do).
    Create folders in the Project and Events libraries and put any projects you are not working on currently, in those folders. This will help a lot. There's a great application for this, called Event Manager X - for the tiny cost it's an invaluable application.
    http://assistedediting.intelligentassistance.com/EventManagerX/
    Unless you cannot edit and playback without it, turn off Background Rendering in Preferences (under Playback) - this will help general performance and you can always render when you need to by selecting the clip (or clips) and pressing Ctrl+R.
    The biggest single improvement I saw in performance was when I upgraded the RAM from 8 GB to 16.
    Andy

  • TS3988 My wife bought an iPad Air and synced contact from our PC (Outlook), now all our contacts in Outlook have gone and we have to go to iCloud to get them, and even then they are a bit jumbled. How do I get my data back onto my PC?

    My wife bought an iPad Air and synced contact from our PC (Outlook), now all our contacts in Outlook have gone and we have to go to iCloud to get them, and even then they are a bit jumbled. How do I get my data back onto my PC?

    If you can see the iCloud contacts in Outlook, you can copy them back to your PC by selecting all the contacts (click one, then press Control-A), then drag and drop the selected contacts to Contacts under My Contacts on the left sidebar of Outlook.
    If you want to copy your calendar back to Outlook, select your iCloud calendar on the left sidebar of Outlook, switch to the list view (select View from the ribbon, then click Change View>List), select your events (click on a single event, then press Control-A), then control drag and drop them to Calendar under My Calendars on the left sidebar.
    Then you can sign out of iCloud on the iCloud control panel and your local copies will still be there.  If you want to add your iCloud email account back to iCloud, you can do this with these settings: http://support.apple.com/kb/HT4864.

  • I have an Ipod Touch 5th Geneation and Windows Vista.  Unfortunately, everytime I plug in my Ipod, it changes my monitors display (resolution and bit from a 32 down to an 8)  How can I prevent this from happening everytime?  I am unable to read my screen

    I have an Ipod Touch 5th gen and I'm running Windows Vista 32-bit Home Premium.  Unfortunately, everytime I plug in my ipod, my screen resolution changes and i go down to an 8 bit from 32...on its own.  I can I prevent this from happening every time I plug it in to my pc?

    All I can suggest is:
    Removing and reinstalling iTunes, QuickTime, and other software components for Windows Vista or Windows 7
    http://support.apple.com/kb/HT1923
    http://support.apple.com/kb/HT1923Seems it is a windows problem.
    You can also try rolling back the driver for your video card.

  • I have Iphone 4 and its lock from at&t, but my contract is getting over in short time. And no longer i want to use this service. So after contract gets over its my right to get factory unlock my iphone 4. so help me

    I have Iphone 4 and its lock from at&t, but my contract is getting over in short time. And no longer i want to use this service. So after contract gets over its my right to get factory unlock my iphone 4.

    jatpri1730 wrote:
    I have Iphone 4 and its lock from at&t, but my contract is getting over in short time. And no longer i want to use this service. So after contract gets over its my right to get factory unlock my iphone 4.
    Unfortunately, AT&T does not provide unlocking.
    Stedman

  • I have Acrobat Pro DC and Adobe PDF Pack and I am still unable to open a PDF file from my outlook.

    I have Acobat Pro DC and Adobe PDK Pack and I am still unable to oepn an PDF filed from my Outlook.  So I have to save it on my computer but then I can only open it in Adobe Reader.  Please help

    Hello courtneym48375386,
    Could you please let me know what version of Outlook are you using.
    Please try opening some other PDF file and check if the same issue persists as this might be a document specific issue.
    Also, what happens if you try opening the file in Acrobat?
    Let me know.
    Regards,
    Anubha

Maybe you are looking for

  • URL Reporting connecting to SAP R/3

    Hello, We have Crystal Reports that connect to ABAP Function Modules in our R/3 4.7 system as the data source. To minimize the number of user prompts, we use URL Reporting to pass all the required parameters for the report. The one thing we have not

  • Join issue - Requestor and Approval data

    Trying to show approval task information with requestor/customer information. We are unable to show approval task information and requestor information in the same result.  It appears that there is no join between requestor/customer and approval data

  • Form access restrict

    Hi All, When I am trying to Open Supplier form in Payables Manager, I am getting error as "Function not available to this responsibility. Change responsibility or contact your system Administrator". Please let me know, if any solution is to solve thi

  • 08/09 MBP (unsure, bought used) errors. Rendered UNUSABLE. Help?

    First my computer suddenly started opening everything in my browser in separate windows instead of tabs, then everything went capslock and I couldn't get it to stop. When I minimized, it took almost a whole minute of watching it slowly drop down to t

  • Printing with black cartridge only

    I'm going to buy HP ENVY 120 all-in-one printer. I will not need the color printing much. Can I use black and white cartridge only without installing color cartriges at all or with empty one or more color cartridges? I just want to save some money on