[Pharo-project] [Slightly OT] Integers smarter than me today
Hello all, I am trying to make a recognizer for a communication protocol that has very likely been errantly documented. Such tasks can be difficult enough with good docs, but I'm not even sure what is supposed to be happening, so it is really interesting. One of the things that I **think** is happening, is that 12-bit two's complement data is included at one point in the stream. The bits are spread over a 16 bit word. Let's assume that I can correctly gather the 12 bits together. Could some kind soul suggest how to go from the resulting SmallInteger to the correct signed value? Smalltalk is adding a wrinkle that I am not sure how to handle: should I set the "missing" bits to 1? Out to bit 31? Any pointers/suggestions would be greatly appreciated. Bill
Assuming these are 12bit signed numbers, then I think the answer is that you should extend the 12th bit out to the 16th bit position. The conversion should be 0..2047 (0x000..0x7FF) => 0..2047, and 0x4095..0x2048 (0xFFF..0x800) => -1..-2048. So, it is probably easier to keep the numbers less than 2096, and for those those bigger than 2047, subtract them from 4096 and negate. David Quoting "Schwab,Wilhelm K" <bschwab@anest.ufl.edu>:
Hello all,
I am trying to make a recognizer for a communication protocol that has very likely been errantly documented. Such tasks can be difficult enough with good docs, but I'm not even sure what is supposed to be happening, so it is really interesting.
One of the things that I **think** is happening, is that 12-bit two's complement data is included at one point in the stream. The bits are spread over a 16 bit word. Let's assume that I can correctly gather the 12 bits together. Could some kind soul suggest how to go from the resulting SmallInteger to the correct signed value? Smalltalk is adding a wrinkle that I am not sure how to handle: should I set the "missing" bits to 1? Out to bit 31?
Any pointers/suggestions would be greatly appreciated.
Bill
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
David, I will try your test, subtract+negate algorithm. On extending the highest bit, what is the reason for stopping at 16? I thought of something similar, but assumed I should cover all bits used in representing a small integer?? Bill -----Original Message----- From: pharo-project-bounces@lists.gforge.inria.fr [mailto:pharo-project-bounces@lists.gforge.inria.fr] On Behalf Of dpharris@telus.net Sent: Thursday, February 11, 2010 8:00 PM To: Pharo-project@lists.gforge.inria.fr Subject: Re: [Pharo-project] [Slightly OT] Integers smarter than me today Assuming these are 12bit signed numbers, then I think the answer is that you should extend the 12th bit out to the 16th bit position. The conversion should be 0..2047 (0x000..0x7FF) => 0..2047, and 0x4095..0x2048 (0xFFF..0x800) => -1..-2048. So, it is probably easier to keep the numbers less than 2096, and for those those bigger than 2047, subtract them from 4096 and negate. David Quoting "Schwab,Wilhelm K" <bschwab@anest.ufl.edu>:
Hello all,
I am trying to make a recognizer for a communication protocol that has very likely been errantly documented. Such tasks can be difficult enough with good docs, but I'm not even sure what is supposed to be happening, so it is really interesting.
One of the things that I **think** is happening, is that 12-bit two's complement data is included at one point in the stream. The bits are spread over a 16 bit word. Let's assume that I can correctly gather the 12 bits together. Could some kind soul suggest how to go from the resulting SmallInteger to the correct signed value? Smalltalk is adding a wrinkle that I am not sure how to handle: should I set the "missing" bits to 1? Out to bit 31?
Any pointers/suggestions would be greatly appreciated.
Bill
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
I guess your problem is already solved, but I could not resist ;) So you have 2^12 = 4096 possible 16 bit words? May I suggest a brute force solution? Make an Array with 2^16 entries to do the translation from 16 bit words to signed values like this: signedValue := translationArray at: word You can make a small script to fill the array. The advantage is that you can check the array's contents to see if you are doing the right thing. If your 2s complement theory should not work out then it is easy to change. HTH Matthias On Thu, Feb 11, 2010 at 10:53 PM, Schwab,Wilhelm K <bschwab@anest.ufl.edu> wrote:
Hello all,
I am trying to make a recognizer for a communication protocol that has very likely been errantly documented. Â Such tasks can be difficult enough with good docs, but I'm not even sure what is supposed to be happening, so it is really interesting.
One of the things that I **think** is happening, is that 12-bit two's complement data is included at one point in the stream. Â The bits are spread over a 16 bit word. Â Let's assume that I can correctly gather the 12 bits together. Â Could some kind soul suggest how to go from the resulting SmallInteger to the correct signed value? Â Smalltalk is adding a wrinkle that I am not sure how to handle: should I set the "missing" bits to 1? Â Out to bit 31?
Any pointers/suggestions would be greatly appreciated.
Bill
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Thanks for bringing this up - yes, David's(??) suggestion did the trick. Beyond that, the key to figuring it out was making dot plots of the data stream so I could see the patterns. There is still an unresolved question or two, but I appear to be getting data, and "the monkey on on their back" as a friend of mine would say. Bill -----Original Message----- From: pharo-project-bounces@lists.gforge.inria.fr [mailto:pharo-project-bounces@lists.gforge.inria.fr] On Behalf Of Matthias Berth Sent: Wednesday, February 17, 2010 3:42 PM To: Pharo-project@lists.gforge.inria.fr Subject: Re: [Pharo-project] [Slightly OT] Integers smarter than me today I guess your problem is already solved, but I could not resist ;) So you have 2^12 = 4096 possible 16 bit words? May I suggest a brute force solution? Make an Array with 2^16 entries to do the translation from 16 bit words to signed values like this: signedValue := translationArray at: word You can make a small script to fill the array. The advantage is that you can check the array's contents to see if you are doing the right thing. If your 2s complement theory should not work out then it is easy to change. HTH Matthias On Thu, Feb 11, 2010 at 10:53 PM, Schwab,Wilhelm K <bschwab@anest.ufl.edu> wrote:
Hello all,
I am trying to make a recognizer for a communication protocol that has very likely been errantly documented. Â Such tasks can be difficult enough with good docs, but I'm not even sure what is supposed to be happening, so it is really interesting.
One of the things that I **think** is happening, is that 12-bit two's complement data is included at one point in the stream. Â The bits are spread over a 16 bit word. Â Let's assume that I can correctly gather the 12 bits together. Â Could some kind soul suggest how to go from the resulting SmallInteger to the correct signed value? Â Smalltalk is adding a wrinkle that I am not sure how to handle: should I set the "missing" bits to 1? Â Out to bit 31?
Any pointers/suggestions would be greatly appreciated.
Bill
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
participants (3)
-
dpharris@telus.net -
Matthias Berth -
Schwab,Wilhelm K