<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Getting unsigned bytes in java</title>
	<atom:link href="http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/feed/" rel="self" type="application/rss+xml" />
	<link>http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/</link>
	<description>Beyond Being Backwards-Compatible</description>
	<lastBuildDate>Fri, 20 Aug 2010 06:54:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Ahti</title>
		<link>http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/comment-page-1/#comment-129</link>
		<dc:creator>Ahti</dc:creator>
		<pubDate>Fri, 22 Aug 2008 13:29:20 +0000</pubDate>
		<guid isPermaLink="false">http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/#comment-129</guid>
		<description>Ahh yes correct. I forgot about the possibility for wrong order of operator precedence, just copied it to java to make sure.</description>
		<content:encoded><![CDATA[<p>Ahh yes correct. I forgot about the possibility for wrong order of operator precedence, just copied it to java to make sure.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vb@vb</title>
		<link>http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/comment-page-1/#comment-130</link>
		<dc:creator>vb@vb</dc:creator>
		<pubDate>Fri, 22 Aug 2008 13:11:06 +0000</pubDate>
		<guid isPermaLink="false">http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/#comment-130</guid>
		<description>You are absolutely right, Ahti - at least this is the most optimal way I could think of as well.

Any other variations such as :
int i = b&lt;0 ? b+256 : b;
are maybe simplier to read, but heavier in terms of performance.

Now to Ellie&#039;s version. It returns 14, however because of operator precedence.
The actual &quot;correct&quot; formula he meant (I think so at least) is:
int num = 16 * ((mySignedByte &amp; 0xf0) &gt;&gt; 4) + (mySignedByte &amp; 0x0f);
Otherwise the last &amp;0x0f will be executed last and trim the 4 MSBs.

It is correct btw, but the question here is why to do it, since it&#039;s actually &amp;0xff.
And the pattern 16 * (X &gt;&gt; 4) is a nice way to obfuscate identity, kinda pseudo-security by obscurity :)


  Vadim.</description>
		<content:encoded><![CDATA[<p>You are absolutely right, Ahti &#8211; at least this is the most optimal way I could think of as well.</p>
<p>Any other variations such as :<br />
int i = b&lt;0 ? b+256 : b;<br />
are maybe simplier to read, but heavier in terms of performance.</p>
<p>Now to Ellie's version. It returns 14, however because of operator precedence.<br />
The actual "correct" formula he meant (I think so at least) is:<br />
int num = 16 * ((mySignedByte &#038; 0xf0) >> 4) + (mySignedByte &#038; 0x0f);<br />
Otherwise the last &#038;0x0f will be executed last and trim the 4 MSBs.</p>
<p>It is correct btw, but the question here is why to do it, since it&#8217;s actually &#038;0xff.<br />
And the pattern 16 * (X >> 4) is a nice way to obfuscate identity, kinda pseudo-security by obscurity <img src='http://ahtik.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>  Vadim.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ahti</title>
		<link>http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/comment-page-1/#comment-131</link>
		<dc:creator>Ahti</dc:creator>
		<pubDate>Fri, 22 Aug 2008 05:16:44 +0000</pubDate>
		<guid isPermaLink="false">http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/#comment-131</guid>
		<description>thanks for the feedback!

Not sure what is pseudocode expected to do. For 0xFE case (signed byte -2) the equation returns 14;
Bit-shifting and multiplying is not needed to trim the sign by half-bytes.

To fix it, mySignedByte&amp;0xf0+(mySignedByte&amp;0x0f) would work (equally to mySignedByte&amp;0xff).

Well, this hard method is behaving like it should - preserves the sign - so no help:

&quot;byte mySignedByte = 0xFE;&quot; is illegal code (cannot convert from int to byte) so one should either use
&quot;(byte) 0xFE;&quot; or &quot;Integer.valueOf(0xFE).byteValue();&quot;.

Both give signed byte (-2 for this case).

Next, Byte.valueOf(mySignedbyte).intValue() gives back -2, signed int.

Byte is signed, however we tweak the bits and intValue() is and should respect the sign bit.</description>
		<content:encoded><![CDATA[<p>thanks for the feedback!</p>
<p>Not sure what is pseudocode expected to do. For 0xFE case (signed byte -2) the equation returns 14;<br />
Bit-shifting and multiplying is not needed to trim the sign by half-bytes.</p>
<p>To fix it, mySignedByte&#038;0xf0+(mySignedByte&#038;0x0f) would work (equally to mySignedByte&#038;0xff).</p>
<p>Well, this hard method is behaving like it should &#8211; preserves the sign &#8211; so no help:</p>
<p>&#8220;byte mySignedByte = 0xFE;&#8221; is illegal code (cannot convert from int to byte) so one should either use<br />
&#8220;(byte) 0xFE;&#8221; or &#8220;Integer.valueOf(0xFE).byteValue();&#8221;.</p>
<p>Both give signed byte (-2 for this case).</p>
<p>Next, Byte.valueOf(mySignedbyte).intValue() gives back -2, signed int.</p>
<p>Byte is signed, however we tweak the bits and intValue() is and should respect the sign bit.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ellie</title>
		<link>http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/comment-page-1/#comment-132</link>
		<dc:creator>Ellie</dc:creator>
		<pubDate>Thu, 21 Aug 2008 09:17:02 +0000</pubDate>
		<guid isPermaLink="false">http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/#comment-132</guid>
		<description>Nice entry!

Though if you have, say:
byte mySignedByte = 0xFE; // = 254 decimal

You can also do (pseudo code) :
int num = 16 * ((mySignedByte &amp; 0xf0) &gt;&gt; 4) + mySignedByte &amp; 0x0f

And of course there is the hard method:
int num = Byte.valueOf(mySignedbyte).intValue();

Sincerely,

Ellie P.</description>
		<content:encoded><![CDATA[<p>Nice entry!</p>
<p>Though if you have, say:<br />
byte mySignedByte = 0xFE; // = 254 decimal</p>
<p>You can also do (pseudo code) :<br />
int num = 16 * ((mySignedByte &amp; 0xf0) &gt;&gt; 4) + mySignedByte &amp; 0x0f</p>
<p>And of course there is the hard method:<br />
int num = Byte.valueOf(mySignedbyte).intValue();</p>
<p>Sincerely,</p>
<p>Ellie P.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
