Is there a difference to send (int)0xFFFFFFFF or (int)0x00FFFFFF using streamConnection

Posted on 2005-05-03
Last Modified: 2008-03-06
i am sending arrays of ARGB pixel through a streamConnection.

To reduce the data i need to send, i decide to delete the alpha channel:

RGB_img[i] = ARGB_img[i] & 0x00FFFFFF;

when i sent the array again, looks like the the time for send the data
is not reduced.

So i wonder if both


send the same amount of data.

Question by:Knightley
    LVL 15

    Expert Comment

    You are writing int (which is always 4 bytes).
    your operation just change the value of that int.
    LVL 15

    Expert Comment

    instead you can extract from the int the 3 first bytes and then send an array of bytes instead of array of int.
    in the other side combine each 3 bytes into one int.
    LVL 19

    Assisted Solution

    by:Jim Cakalic
    On the sending side, that would look something like:

        DataOutputStream out = ...
        int[] argb = ...
        for (int i = 0; i < argb.length; ++i) {
            out.writeByte((byte)argb[i] >> 16);
            out.writeByte((byte)argb[i] >> 8);

    And on the receiving side:

        DataInputStream in;
        int[] argb = new int[in.readInt()];
        for (int i = 0; i < argb.length; ++i) {
            argb[i] = in.readByte() << 16 | in.readByte() << 8 | in.readByte();

    That's all from memory so mea culpa if it doesn't compile :-) That seems like a long way around for saving one byte. I guess you plan to do that a lot of times, eh?

    LVL 15

    Expert Comment

    argb[i] = in.readByte() << 16 | in.readByte() << 8 | in.readByte();
    should be changed to
    argb[i] = ((in.readByte()  &0xff) << 16) | ((in.readByte() &0xff) << 8) | (in.readByte() & 0xff);
    To avoid left bit expansion when casting byte to int.
    LVL 1

    Author Comment

    is there a way only to send 1/2 byte?

    like "0xF" ???
    LVL 15

    Accepted Solution

    the smallest unit is byte, but you can combine two values in one byte and then extract in a similar way in the other side.
    byte value1
    byte value2
    byte combined = (value1 >>> 4) & 0xf | (value2 & 0xf); // >>> is shift right as is (without mainting the sign).

    and then on the other side
    value1 = combined & 0xf;
    value2 = (combined >> >4) & 0xf;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now