byte question

I have a program loaded into a memory array. It starts at location memory[2027]. Each index is 4 bytes long. How would I get the first byte? 1B?

int main()
{
unsigned int memory [250144];
unsigned int *pointer;
unsigned int array[10];
unsigned int sum =0;
unsigned int PC =0;
unsigned int IP =0;
int EA=0;
unsigned int temp =0;
int R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15; // Declare 16 registers
// find location [2027]
for (int j = 1; j <= 10; j++)
{
array[j]= j;
}
pointer = &memory[2027];
// Load the program into memory
memory[2027] = 0x1B111B44;
memory[2028] = 0x1B664180;
memory[2029] = 0x000A05C0;
memory[2030] = 0x5A64C414;
memory[2031] = 0x40044680;
memory[2032] = 0xC5060C00;
memory[2033] = 0x0FFFFF;
PC = 8108;
IP = 8110;
// fetch 1 byte of info at a time
// keep track of indices along with actual addresses

temp = (memory[2027]/4);
cout << temp << " ";

/*switch(temp)
{
case 1: subtract(temp); break;
}*/

return 0;
}
One byte is 8 bits.
You can "bitshift" to get the bits you're interested in into the low 8 positions, then you can mask out the unwanted bits with a bitwise and operation.

1
2
3
4
5
6
unsigned int v = 0x12345678;

unsigned int a = (v >> 24) & 0xFF;  // 0x12
unsigned int b = (v >> 16) & 0xFF;  // 0x34
unsigned int c = (v >>  8) & 0xFF;  // 0x56
unsigned int d = (v      ) & 0xFF;  // 0x78 
yay thanks!!
Topic archived. No new replies allowed.