Question

I'm using byte arrays (of size 2 or 4) to emulate the effect of short and int data types. Main idea is to have a data type that support both char and int types, however it is really hard for me to emulate arithmetic operations in this way, since I must do them in bit level. For those who do not follow:

The int representation of 123 is not equal to the byte[] of {0,1,2,3} since their bit representations differ (123 representation is 00000000000000000000000001111011 and the representation of {0,1,2,3} is 00000000000000010000001000000011 on my system.

So "int of 123" would actually be equivalent to "byte[] of {0,0,0,123}". The problems occur when values stretch over several bytes and I try to subtract or decrement from those byte arrays, since then you have to interact with several different bytes and my math isn't that sharp.

Any pseudo-code or java library suggestions would be welcome.

Was it helpful?

Solution

Unless you really want to know what bits are being carried from one byte to the next, I'd suggest don't do it this way! If it's just plain math, then convert your arrays to real short and int types, do the math, then convert them back again.

If you must do it this way, consider the following:

Imaging you're adding two short variables that are in byte arrays.

The first problem you have is that all Java integer types are signed.

The second is that the "carry" from the least-significant-byte into the most-significant-byte is best done using a type that's longer than a byte because otherwise you can't detect the overflow.

i.e. if you add two 8-bit values, the carry will be in bit 8. But a byte only has bits 0..7, so to calculate bit 8 you have to promote your bytes to the next appropriate larger type, do the add operation, then figure out if it resulted in a carry, and then handle that when you add up the MSB. It's just not worth it.

BTW, I did actually have to do this sort of bit manipulation many years ago when I wrote an MC6809 CPU emulator. It was necessary to perform multiple operations on the same operands just to be able to figure out the effect on the CPU's various status bits, when those same bits are generated "for free" by a hardware ALU.

For example, my (C++) code to add two 8-bit registers looked like this:

void mc6809::help_adc(Byte& x)
{
    Byte    m = fetch_operand();

    {
        Byte    t = (x & 0x0f) + (m & 0x0f) + cc.bit.c;
        cc.bit.h = btst(t, 4);          // Half carry
    }

    {
        Byte    t = (x & 0x7f) + (m & 0x7f) + cc.bit.c;
        cc.bit.v = btst(t, 7);          // Bit 7 carry in
    }

    {
        Word    t = x + m + cc.bit.c;
        cc.bit.c = btst(t, 8);          // Bit 7 carry out
        x = t & 0xff;
    }

    cc.bit.v ^= cc.bit.c;
    cc.bit.n = btst(x, 7);
    cc.bit.z = !x;
}

which requires that three different additions get done on different variations of the operands just to extract the h, v and c flags.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top