Question

I have a list of ints:

public List<int> binaryVector;

However I want to read in only the first 8 bytes of that integer and store it as a variable. Then I want to do the same for the next 24 bytes of that integer after that. All ints in the binary vector are of 32 bit. Any Idea on how I can do this?

the first 24 bits of the int is considered the value that the instruction is holding. the last 8 bits is a numerical value that if looked up in a hash table, gets translated to an instruction

Example:

lda #$5 // load the value into the accumulator

when converted to binary it is put into 32 bit int format and stored in the binary vector. first 24 bits is the value stored, in this case the value 5. the last 8 bits are reserved for the instruction.

I want to read pieces of the 32 bit int and store them as variables. for example i want to read the first 24 bits of the example given above and store it as an int variable which should equal 5.

how i store instructions:

int IMM = 16777216;

int inst = instructionValue;

inst = inst | IMM;


inst = inst << 24;

inst = inst | value; 

the 24 bits are the value, the 25 bit is like a boolean either 1 or 0 telling me if the value is an immediate. the last 7 bits are for the instruction

Was it helpful?

Solution

My understanding is that you want to extract the lower 24 bits from the integers into param and the upper 8 bits into command.

You can do that in the following way:

var param = binaryVector[i] & 0xffffff;
var command = (binaryVector[i] >> 24) & 0xff;

Feel free to replace i in the code with your "instruction pointer"

Hint: Be careful in the construction of the instruction integer that param not exceeds 24 bits. Your code should be protected for this case with a mask or you would destroy your command byte:

inst = inst | value & 0xffffff; 

OTHER TIPS

You can create struct and define set variable in there:

Here is an example:

struct SimpleStruct
{
    private int xval;
    public int X
    {
        get 
        {
            // write what you need here
        }
        set 
        {
           // set your variable here
        }
    }

}

Then you can define your list :

public static List<SimpleStruct> myList{ get; private set; }

Or if you want to make it with method you can use such example:

public byte ReadFirstNBits(int n)
{
    Contract.Requires(n >= 0 && n <= 8, "Code contract may be too slow for your needs");
    byte val = base.ReadByte();
    return val >> (8 - n);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top