I am working on a project where I am reading memory locations and need to output their hex value in ASCII.
The language gives me a 16 bit word length, so I have a need to divide to grab a nibble at a time to convert to hex. Unfortunately, the language only offers and, or, not, and add for mathematical/logical functions.
I've figured I can create the desired effect by left shifting and testing for a negative flag to add a 1 to the end after shifting, but I'm figuring there has to be a better method for doing this.
Any insight would be appreciated.
So the original method I used worked. I also came up with another, incase anyone ever has this problem again.
I built a subroutine that evaluates 4 bits at a time and creates a number based on the evaluation, for some C style pseudo code it looks like this:
16bitSignedInt bin; //binary being analyzed
int value; //number being built
for (int i = 0; i < 4; i++) // while 0-3, for each nibble of the 16 bits
{
if (bin.bit15 = 1)
value += 8; // dominate bit in nibble
bin <<= 1; // left shift 1
if (bin.bit15 = 1)
value += 4; // 2nd bit in nibble
bin <<= 1; // left shift 1
if (bin.bit15 = 1)
value += 2; // 3rd bit in nibble
bin <<= 1; // left shift 1
if (bin.bit15 = 1)
value += 1; // last bit in nibble
bin <<= 1; // left shift 1
//do work with value
}
Crude, but effective.