Java - Efficient way to copy bit array to 32-bit int array

Posted on 2009-04-28
Last Modified: 2012-05-06
I'm working with some imaging stuff.  I have an array of bytes, but each bit represents a pixel as this is a monochrome image.

The buffer into which I need to copy is for a CMYK image.  It expects 32 bits/pixel.

I think I need to turn each 0 bit into 0x00000000 and each 1 bit into 0x FFFFFFFF.

I could run a loop that did bitwise math.  But is there an EFFICIENT way of doing this?


Question by:Daniel Wilson
    LVL 17

    Assisted Solution

    The most efficient way to do this is by using bitwise math. Your code would look something like the attached snippet. My source for saying the most efficient way is with bitwise operators is that is how BitSet gets individual bits in an int. BitSet is in java.util and is designed to store booleans in integers. To extract a specific bit it uses bitwise operators. This leads me to think this is the quickest way it could be done.

    However, there may be a native to a particular OS way of doing it faster. If performance is a really big concern maybe you could write the conversion in C++ then use JNI to call that method.
    int packedData;
    int black = 0x00000000;
    int white = 0xFFFFFFFF;
    for(int i = 0; i < 16; i++)
    	int newColor = ((packedData & (1 < i)) != 0) ? black : white;

    Open in new window

    LVL 86

    Assisted Solution

    >>but each bit represents a pixel as this is a monochrome image.

    So you're saying that each element of your array contains info for 8 pixels?
    LVL 32

    Author Comment

    by:Daniel Wilson
    >>So you're saying that each element of your array contains info for 8 pixels?

    Yes.  This is a stencil mask in a PDF.
    LVL 86

    Assisted Solution

    OK. And it has to end as one int per pixel?
    LVL 32

    Accepted Solution

    As it turns out, I was using the wrong color model. I shouldn't have been trying to expand each bit to a 32-bit int after all.

    Thanks for your assistance, though!
    LVL 86

    Expert Comment

    I thought it sounded a little strange ;-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    FizzBuzz challenge 9 64
    canBalance challenge 34 55
    Problem to setup 18 66
    Safari, Firefox, IE, Java, Plugin, Java Security error 13 26
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now