Reputation: 57
I have a memory location at 0x31011 that stores 16, 1 bit symbols' data. Each of these symbols has a different starting bit location of where their information is stored:
1st Symbol: 0x31011 --> Starting bit at bit 0
2nd Symbol: 0x31011 --> Starting bit at bit 1
3rd Symbol: 0x31011 --> Starting bit at bit 2
...
14th Symbol: 0x31011 --> Starting bit at bit 13
15th Symbol: 0x31011 --> Starting bit at bit 14
16th Symbol: 0x31011 --> Starting bit at bit 15
The hex to binary conversion of 0x31011 is: 110001000000010001
I would love some enlightenment on my understanding of how one can retrieve the address for each of these symbols, if that is even possible. My understanding is that I will have to use the base address at 0x31011 for each of these symbols and then extract the value from this location and then use bit masking with the AND operator and then shift right. So my original thought of being able to call a subaddress of the base address is not possible.
For example, if I want to find the value of the 1st symbol at bit 0, assume value at 0x31011 is 0000001100110001:
1: Retrieve the value at 0x31011--> 0000001100110001
2: Apply a mask of: C7 --------------> 0000000011000111
-------------------------------------------------------------------------------- &
3: Find value of 1st symbol 0000000000000001
If I want to find the value of the 2nd symbol at bit 1, assume value at 0x31011 is 00000001100110001:
1: Retrieve the value at 0x31011--> 0000001100110001
2: Apply a mask of: C6 --------------> 0000000011000110
-------------------------------------------------------------------------------- &
3: Find value of 2nd symbol ------> 0000000000000000
4: Right shift result >> 1 ----------> 0000000000000000
If I want to find the value of the 3rd symbol at bit 2, assume value at 0x31011 is 00000001100110001:
1: Retrieve the value at 0x31011--> 0000001100110001
2: Apply a mask of: C6 --------------> 0000000011000110
-------------------------------------------------------------------------------- &
3: Find value of 3rd symbol ------> 0000000000000000
4: Right shift result >> 2 ----------> 0000000000000000
....
If I want to find the value of the 16th symbol at bit 15, assume value at 0x31011 is 00000001100110001:
1: Retrieve the value at 0x31011--> 0000001100110001
2: Apply a mask of: 80C6 -----------> 1000000011000110
-------------------------------------------------------------------------------- &
3: Find value of 16th symbol ------> 0000000000000000
4: Right shift result >> 15 ---------> 0000000000000000
Upvotes: 0
Views: 1822
Reputation: 364468
Memory is normally only byte-addressable, not bit-addressable. To represent the address of a single bit, you need a regular address and a bit offset.
However, some ISAs do have some capacity for bit-addressable memory. The 8051 microcontroller's bit-addressable memory is one notable example. But the bit set/clear/complement and branch-on-bit instructions are only available with "direct" addressing modes, so you can't pass around an address of a bit, unless you use self-modifying code. Addresses 00-7F are full bytes when used with byte load/store instructions, but bits of the first 16 bytes when used with bit instructions.
Outside of special ISA features like instructions that can use bit addressing, you have a software problem. A single bit is not directly addressable.
You can only read it as part of the byte or word that contains it.
You can certainly represent its location as a normal byte-address + a bit offset, though. For example, a C function like this is easy to implement on any normal ISA that has pointer registers and a right shift:
bool get_bit(const char *location, int bit_offset) {
unsigned char tmp = *location;
return (tmp >> bit_offset) & 1;
}
For example on x86-64:
movzx eax, byte [rdi] ; load the byte
bt eax, esi ; CF = bit at position ESI
setc al ; set AL = CF
ret
On a machine where real addresses don't need as many bits as a register is wide, you could encode you bit-address into a single integer value with the bit-offset in the low 3 bits. (Or more bits for machines with larger bytes). Then to use it, you'd right shift to get an actual machine address and also mask to extract the bit-offset for use as a right-shift count.
Upvotes: 1