Packing bytes into chars

Discussion in 'Java' started by Chris, Sep 2, 2003.

  1. Chris

    Chris Guest

    What's the most efficient way to pack two bytes into a char? This is the
    best I could come up with, but it looks terrible:

    byte b0 = 0;
    byte b1 = 1;

    char ch = (char)(((int)b0 << 8) | b1);

    This appears to have two explicit casts and one implicit one. It's also
    really slow.
    Chris, Sep 2, 2003
    #1
    1. Advertising

  2. Chris

    Eric Sosman Guest

    Chris wrote:
    >
    > What's the most efficient way to pack two bytes into a char? This is the
    > best I could come up with, but it looks terrible:
    >
    > byte b0 = 0;
    > byte b1 = 1;
    >
    > char ch = (char)(((int)b0 << 8) | b1);
    >
    > This appears to have two explicit casts and one implicit one. It's also
    > really slow.


    Question: Why do you think it's slow? Have you made
    measurements?

    Observation: Although this will work for the particular
    values shown in the example, it will fail rather badly for
    bytes whose sign bit is set.

    --
    Eric Sosman, Sep 2, 2003
    #2
    1. Advertising

  3. Chris

    Roedy Green Guest

    On Tue, 2 Sep 2003 11:50:38 -0500, "Chris" <> wrote
    or quoted :

    >char ch = (char)(((int)b0 << 8) | b1);
    >
    >This appears to have two explicit casts and one implicit one. It's also
    >really slow.


    You can prune out the (int) cast which is implicit. You can prune out
    one level of () which are implied by precedence. However the generated
    code is the same. That is as tight as I can make it.


    char ch = (char)( b0 << 8 | b1 );


    Note, this code will only work if the high bit is off on b1
    Otherwise you have to write:

    char ch = (char)( b0 << 8 | b1 & 0xff );
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 2, 2003
    #3
  4. On Tue, 02 Sep 2003 11:50:38 -0500, Chris wrote:

    > What's the most efficient way to pack two bytes into a char? This is the
    > best I could come up with, but it looks terrible:
    >
    > byte b0 = 0;
    > byte b1 = 1;
    >
    > char ch = (char)(((int)b0 << 8) | b1);
    >
    > This appears to have two explicit casts and one implicit one. It's also
    > really slow.


    Worse, it doesn't work. The implict promotion of b1 to int will cause sign
    extension and overwrite b0's value if b1 is negative. Try:

    char ch = (char) ( (b0 << 8) | (b1 & 255) );

    The sign extension of b0 gets lost again in the cast to int, so that's not an
    issue.

    Steve
    Steve Horsley, Sep 2, 2003
    #4
  5. Chris wrote:
    > What's the most efficient way to pack two bytes into a char? This is the
    > best I could come up with, but it looks terrible:
    >
    > byte b0 = 0;
    > byte b1 = 1;
    >
    > char ch = (char)(((int)b0 << 8) | b1);
    >
    > This appears to have two explicit casts and one implicit one. It's also
    > really slow.


    Well, this one is slower but it is correct for all byte values:

    char pack(byte hi, byte low)
    {
    int i = hi >> 7 == 0 ? hi : (256 + hi);
    int i2 = low >> 7 == 0 ? low : (256 + low) ;

    char c = (char)((i << 8) + i2);
    return c;
    }

    --
    Daniel Sjöblom
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Sep 2, 2003
    #5
  6. Chris

    Roedy Green Guest

    On Wed, 03 Sep 2003 00:59:53 +0300, Daniel Sjöblom
    <_NOSPAM> wrote or quoted :

    >int i = hi >> 7 == 0 ? hi : (256 + hi);
    > int i2 = low >> 7 == 0 ? low : (256 + low) ;


    you don't need that for the high byte since there is nothing extended
    to cause trouble.

    you can compute the low byte more quickly with

    int i2 = low & 0xff;


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 2, 2003
    #6
  7. Roedy Green wrote:
    > On Wed, 03 Sep 2003 00:59:53 +0300, Daniel Sjöblom
    > <_NOSPAM> wrote or quoted :
    >
    >
    >>int i = hi >> 7 == 0 ? hi : (256 + hi);
    >> int i2 = low >> 7 == 0 ? low : (256 + low) ;

    >
    >
    > you don't need that for the high byte since there is nothing extended
    > to cause trouble.
    >
    > you can compute the low byte more quickly with
    >
    > int i2 = low & 0xff;


    True, true. I was a slight bit embarassed after reading your solution :)
    But anyway, what on earth was the rationale behind making byte signed?
    --
    Daniel Sjöblom
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Sep 3, 2003
    #7
  8. Chris

    Roedy Green Guest

    On Wed, 03 Sep 2003 16:47:29 +0300, Daniel Sjöblom
    <_NOSPAM> wrote or quoted :

    >But anyway, what on earth was the rationale behind making byte signed?


    I think that was goofy too. I want a signed byte perhaps 1% as often
    as an unsigned one.

    This probably was inherited from C where they could not make up their
    mind. It started out signed since chars were 7 bits in the early days
    of the PDP-11.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 3, 2003
    #8
  9. On Wed, 03 Sep 2003 16:47:29 +0300, Daniel Sjöblom wrote:

    > Roedy Green wrote:
    > But anyway, what on earth was the rationale behind making byte signed?


    I still think someone must have put LSD in the coffee machine.

    Steve
    Steve Horsley, Sep 3, 2003
    #9
  10. Chris

    Eric Sosman Guest

    Steve Horsley wrote:
    >
    > On Wed, 03 Sep 2003 16:47:29 +0300, Daniel Sjöblom wrote:
    >
    > > Roedy Green wrote:
    > > But anyway, what on earth was the rationale behind making byte signed?

    >
    > I still think someone must have put LSD in the coffee machine.


    Least Significant Digit? I don't get it ...

    --
    Eric Sosman, Sep 3, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David N. Welton

    bytes, chars, and strings, oh my!

    David N. Welton, Oct 5, 2005, in forum: Java
    Replies:
    5
    Views:
    440
    Thomas Fritsch
    Oct 6, 2005
  2. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,246
    Tim Rentsch
    Sep 23, 2005
  3. Hongyu
    Replies:
    9
    Views:
    887
    James Kanze
    Aug 8, 2008
  4. Michael Henry

    Practical packing for structs of bytes

    Michael Henry, Sep 17, 2010, in forum: C Programming
    Replies:
    12
    Views:
    1,517
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    214
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page