Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Packing bytes into chars

Reply
Thread Tools

Packing bytes into chars

 
 
Chris
Guest
Posts: n/a
 
      09-02-2003
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 << | b1);

This appears to have two explicit casts and one implicit one. It's also
really slow.


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-02-2003
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 << | 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-02-2003
On Tue, 2 Sep 2003 11:50:38 -0500, "Chris" <(E-Mail Removed)> wrote
or quoted :

>char ch = (char)(((int)b0 << | 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.
 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      09-02-2003
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 << | 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 << | (b1 & 255) );

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

Steve
 
Reply With Quote
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      09-02-2003
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 << | 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 << + i2);
return c;
}

--
Daniel Sj÷blom

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-02-2003
On Wed, 03 Sep 2003 00:59:53 +0300, Daniel Sj÷blom
<(E-Mail Removed)_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.
 
Reply With Quote
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      09-03-2003
Roedy Green wrote:
> On Wed, 03 Sep 2003 00:59:53 +0300, Daniel Sj÷blom
> <(E-Mail Removed)_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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-03-2003
On Wed, 03 Sep 2003 16:47:29 +0300, Daniel Sj÷blom
<(E-Mail Removed)_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.
 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      09-03-2003
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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-03-2003
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 ...

--
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Practical packing for structs of bytes Michael Henry C Programming 12 09-23-2010 12:14 AM
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
bytes, chars, and strings, oh my! David N. Welton Java 5 10-06-2005 11:37 AM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM



Advertisments