I have a method in c# with only 1 parameter (it is an override so I cannot change its signature to incorporate more parameters):
read_address(long adr)
where adr is the address memory I want to read, but I have to pass the address and indicating at the same time if the address to read is 16 or 32 bit and if 32 bits, furthermore I have to indicate if I want to read upper or lower word so I would like to know an efficient way to do this using parameter adr.
I have thought to build a mask, for example, If I want to read address 614 (decimal), I can add two digits before or back:
10614 : first digit 1 indicates size=32bit and second one 0=lower word
11614 : first digit 1 indicates size=32bit and second one 1=upper word
for indicate 16 bit, no necessary to indicate lower or upper so:
00614 = 614 will indicate 16-bit.
Also I can done this by putting those two digits back:
61410
61411
So when I receive this number through addr parameter I have to parse it to know if this is a 16 or 32 bit and in case of 32 if it is lower or upper word.
EDITED:
I think I haven’t explained well…
For example, figure that this method, read_address, receive and address (addr) to read. THis is called from another method, method_A, which knows if this is 16 or 32 bit and if 32 bit it splits into two word. Better an example, for read 614:
Method_A(....)
{
if 16-bit then
{
call read_address(620) // Supose 620 is 16 bit
}
if 32-bit then
{
// suppose 614 is 32 bit so split into two reads
call read_address(61410) // to read first word
call read_address(61411) // to read second word
}
}
so in read_address I have to know if it is 16 or 32 bit and if 32-bit, I also have to know if it is lower or upper word.
read_address(long addr)
{
// decode if addr is 16 or 32 bit and if 32 bit, decode if lower
// or upper word and do some stuff
// So suppose it arrives 61410... how to decode it in order to know,
// address to read is 614 and is 32-bit (1) and I want to read lower word (0)
}
You mention the “upper/higher” word only counts when addr specifies a 32-bit address – I hope you meant it only counts on 16-bits? (if not, how would you know whether the value “10” is a mask or an adress?)
I wouldn’t use decimal positions for the mask, but rather split the 64 bits into two 32 bit numbers (integers), and use binary flags.
The example 614 as a 64-bit number in binary would then be (I hope I got the endianness right, but it should illustrate the situation):
You can then use a
[Flags] enumto define the mask, and cast the mask-integer to that enum. Here’s an example of creating and parsing the address: