Search code examples
cpointerslong-integerallocation

long int high and low bits pointers


I'm trying to implement improved sequential multiplication algorithm in C. where the size of the product register is two times the size of the multiplicand and multiplier. in C an int is of 4 bytes and a long int makes it 8 bytes. I wanted to access the higher and lower 32-bits independently. so I pointed the lower and upper bits like:

long long int product = 0;
int* high = &product;
int* low = &product;
low++;

but this didn't work because I thought that if an int is allotted 4 bytes then a long int would be allotted 8 bytes and the pointer would be pointing to the MSB of the allocated memory. I'm not sure if this is actually how allocation is done. can anyone please help me clear this confusion.

I solved the problem using by doing this:

long long int product=0;
int* low = &product;
int* high = &product;
high++;

but I'm still confused that why is it working correctly;


Solution

  • You are probably using a computer that is Little-Endian. On a little-endian machine, the least significant byte is first.