VMWare Inc Interview Question
Software EngineersCountry: United States
Interview Type: Written Test
I think all you need to do for this one is isolate the high 16 bits to see if they are all 0 or 1.
bool X86IsCanonicalAddress(int a) {
// Return true if lowest 48 bits are equal and highest 16 bits are all 1 or all 0
// Assumes compiler treats int as unsigned 64bit and does arithmetic shifting
// Isolate the highest 16 bits.
// If all 1, result will be -1 due to arithmetic type shifting.
// If all 0, result is 0
int high16 = a >> 48;
if(high16 == -1 || high16 == 0) {
return true;
}
return false;
}
- Mukesh July 07, 2016