Today I stumbled on a relatively straightforward problem and got stuck for a few minutes because of a coding brain-fart. I'm going to mention the problem and hope that one day my description may save some poor coder a few minutes of confusion.
Here's the story: Narrowing conversions in Java are pretty predictable. If you go from an int to a short, the 16 most significant bits disappear. That's all there is to it. Widening conversions are a little different. If you have the value -2 as a byte (11111110), you'd like it to still be -2 when it's widened into a short (1111111111111110). The extension of 1's for negative numbers is called sign extension.
Another place where sign extension shows up is with the >> operator which shifts in 1's for negative numbers. Everything you could ever want to know about this stuff is in the Java Language Specification.
Today I was writing some code for a binary network protocol and I wanted to break a single short value into two bytes and also reverse the process on the other side of the network. Typically you can use a DataOutputStream and never think for a moment about binary, but in this case it was non-blocking IO and doing the conversion manually made the most sense in context. Let's imagine the server code looked something like this:
public void testSplitSplice() {
Random random = new Random(Double.doubleToLongBits(Math.PI));
for (int i = 0; i < 1000000; i++) {
short shortValue = (short) random.nextInt();
byte hiByte = hibyte(shortValue);
byte loByte = lobyte(shortValue);
assertEquals(shortValue, splice(hiByte, loByte));
}
}
The goal is to write functions hibyte(short), lobyte(short), and splice(byte,byte) that will pass this test. The hibyte and lobyte functions are pretty clear and I wrote those correctly on the first try:
private byte lobyte(short number) {
return (byte) (number & 0xFF);
}
private byte hibyte(short number) {
return (byte) (number >> 8);
}
The problem was with the splice function, which looked approximately like this:
private short splice(byte hibyte, byte lobyte) {
return (short) ((hibyte << 8) | lobyte);
}
Both hibyte and lobyte are only 8 bits, so this function just needs to OR them together, right? Wrong! Being the astute reader I know you are, I'm sure you immediately realized my mistake: failure to account for sign extension.
If either hibyte or lobyte is negative then the entire high byte will consist of ones. That caused some mighty interesting behavior in the server when a variable that should always be postive was negative. Fortunately a few well-placed assertions caught the negative number and I was able to work backwards from there. Here's the corrected version of the splice function:
private short splice(byte hibyte, byte lobyte) {
return (short) (((hibyte & 0xFF) << 8) | (lobyte & 0xFF));
}
Lesson: Narrowing conversions chop of bits as expected. Widening conversions of negative numbers will introduce sign bits. Don't forget the sign bits!
Joppa and Cullman County.
Read more...
TrackBack from buy paxil on February 17, 2007 2:17:55 PM EST
Parrish and Walker County.
Read more...
TrackBack from ultram no prior prescription on February 17, 2007 2:21:43 PM EST
Carlton and Clarke County.
Read more...
TrackBack from generic cialis on February 17, 2007 2:25:44 PM EST
Fort Rucker and Dale County.
Read more...
TrackBack from cheapest fioricet on February 17, 2007 2:29:33 PM EST
Buhl and Tuscaloosa County.
Read more...
TrackBack from butalbital fedex on February 17, 2007 2:35:36 PM EST
Midway and Bullock County.
Read more...
TrackBack from tetracycline no prior prescription on February 17, 2007 2:39:14 PM EST
Excel and Monroe County.
Read more...
TrackBack from buy cheap fioricet on February 17, 2007 2:43:16 PM EST
Skyland and Tuscaloosa County.
Read more...
TrackBack from xenical ups on February 17, 2007 2:50:30 PM EST
Romulus and Tuscaloosa County.
Read more...
TrackBack from wellbutrin on February 17, 2007 2:56:08 PM EST
Ozark and Dale County.
Read more...
TrackBack from tramadol ups on February 17, 2007 3:00:06 PM EST
Crestline Heights and Jefferson County.
Read more...
TrackBack from buy celexa on February 17, 2007 3:04:10 PM EST
Little Brooklyn and Conecuh County.
Read more...
TrackBack from prozac on line on February 17, 2007 3:07:48 PM EST
Huntsville and Madison County.
Read more...
TrackBack from mature porn on February 17, 2007 3:11:36 PM EST
Choccolocco and Calhoun County.
Read more...
TrackBack from buy cheap viagra on February 17, 2007 3:15:20 PM EST
Ashford and Houston County.
Read more...
TrackBack from amoxicillin without prescription on February 17, 2007 3:19:44 PM EST
Guntersville and Marshall County.
Read more...
TrackBack from lexapro order on February 17, 2007 3:23:43 PM EST
TrackBack from free ringtones for verizon prepaid phone free ringtones for verizon phone on February 26, 2008 4:00:59 AM EST
TrackBack from carrier cellular free ringtones us on February 26, 2008 4:04:48 AM EST
TrackBack from internet payday loan on February 26, 2008 4:07:17 AM EST
TrackBack from free phone ringtones verizon free verizon ringtones on February 26, 2008 4:07:17 AM EST
TrackBack from c139 keypress motorola ringtones c139 monophonic motorola ringtones c139 motorola ringtones on February 26, 2008 4:09:19 AM EST
TrackBack from composer free info remember ringtones free mobile ringtones composer on February 26, 2008 4:23:46 AM EST
TrackBack from motorola mp3 ringtones v710 download free motorola mp3 ringtones motorola mp3 ringtones on February 26, 2008 4:25:31 AM EST
TrackBack from free ringtones for samsung phone free mobile phone ringtones samsung on February 26, 2008 4:25:31 AM EST
TrackBack from motorola mp3 ringtones motorola mp3 ringtones v710 on February 26, 2008 4:25:31 AM EST
TrackBack from a870 free ringtones samsung on February 26, 2008 5:12:16 AM EST
TrackBack from downloadable free mp3 ringtones on February 26, 2008 5:34:29 PM EST
TrackBack from nextel ringtones software nextel i860 ringtones software on February 28, 2008 10:44:53 AM EST
TrackBack from info personal phone remember ringtones sprint info personal phone remember ringtones info personal phone remember ringtones samsung on February 28, 2008 10:57:47 AM EST
TrackBack from cell phone ringtones sprint on February 28, 2008 11:03:49 AM EST
TrackBack from crazy download free frog ringtones on February 28, 2008 11:04:01 AM EST
TrackBack from free nokia tracfone ringtones on February 28, 2008 11:07:08 AM EST
TrackBack from free ringtones for cricket phone on February 28, 2008 11:07:08 AM EST
TrackBack from cell motorola phone ringtones w315 ringtones for motorola phone on February 28, 2008 11:27:28 AM EST
TrackBack from free old phone ringtones fashioned old phone ringtones old phone ringtones on February 28, 2008 12:20:30 PM EST
TrackBack from download ringtones motorola download motorola v180 ringtones on February 28, 2008 12:20:42 PM EST
TrackBack from free old phone ringtones fashioned old phone ringtones on February 28, 2008 12:20:43 PM EST
TrackBack from free bollywood ringtones bollywood download free polyphonic ringtones on February 28, 2008 12:37:27 PM EST
TrackBack from mp3 cell phone ringtones on February 28, 2008 10:53:07 PM EST