Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:50:16+00:00 2026-06-04T12:50:16+00:00

I have been playing with bitwise operations in order to compactly store information about

  • 0

I have been playing with bitwise operations in order to compactly store information about objects, what I intend to do is have a short[][] that stores two pieces of information per entry, that is that the first set of bits (either 8 or 4) contains info and then the remaining bits (either 8 or 12 respectively) store the rest.

In the code below I demonstrate the two examples I mentioned, questions to follow;

private void test1() {
    // This test takes a 16 bit binary number and breaks it into two
    // bytes of 8 bits. It then takes the bytes and sticks them back
    // together then outputs their decimal value
    String st = "0011111100110111";
    short s = Short.parseShort(st,2);
    byte[] ba = new byte[] {
        (byte)(s & 0xFF),
        (byte)((s >>> 8) & 0xFF)
        };

    System.out.println(s);
    System.out.println(ba[0]);
    System.out.println(ba[1]);

    byte b0 = ba[0];
    byte b1 = ba[1];

    short sh = (short)((b1 << 8) | b0);

    System.out.println(sh);
}

private void test2() {
    // This test takes two shorts and sticks them together in a
    // 4 bit 12 bit configuration within a short, it then breaks
    // them apart again to see if it worked!
    short s0 = 4095;
    short s1 = 15;

    short sh = (short)((s1 << 12) | s0);

    System.out.println(sh);

    short[] sa = new short[] {
        (short)(sh & 0xFFF),
        (short)((sh >>> 12) & 0xF)
    };

    System.out.println(sa[0]);
    System.out.println(sa[1]);

}

My main concern is that in test2() I expected to only be able to use signed values, however I seem to be able to use the values 4095 for the 12 bit and 15 for the 4 bit (I expected ranges to be -2048 to 2047 and -8 to 7), how come it works with these values, what am I missing?

Also another concern, why can’t I use 1011111100110111 in test1()?

Lastly, is this a good idea to store information in this manner? The array will be approx 500×200 or 1000x 500 something like that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:50:19+00:00Added an answer on June 4, 2026 at 12:50 pm

    The reason why 4095 works in the second line, is because you are sign extending it before printing it. If you understand that unsigned 4095 is exactly the same bits as -2048, it just matters how you interpret them.

    If you were printing out a 12-bit signed value, it would be this: 'b1111_1111_1111, which would be interpreted as -2048. However you’re casting this to a short which adds another 4 bits at the end: 'b0000_1111_1111_1111. 4095 fits fine inside this value.

    The same applies to 15/-8, you’re casting it to a larger value before printing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been playing around with creation of pdf documents for a project that
I have been playing about with iText to try get a list of embedded
I have been playing with image flood fill that I found here on Stack
I have been playing around with some debugging and wrote some C code that
I have been playing a bit with the memory in order to be a
I have been playing around with a search control and i have noticed that
I have been playing around with the concept of 'module' that some mvc frameworks
I have been playing around a bit with a fairly simple, home-made search engine,
I have been playing around with the MVP pattern using winforms for the last
I have been playing around with the Rhino ETL library and really like the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.