Microsoft Interview Question
Software Engineer / DevelopersTeam: Windows Phone
Country: United States
Interview Type: Phone Interview
on mask creation step, instead of creating mask on every run i used a simple lookup table with prebuilt masks
masks[] = {0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F}
but i was not sure which technique is faster (using lookup table or creating it at runtime)... can anyone comment on it ?
I guess look up is better:
new_dest_address = (*new_src_address & masks[leftover_bits-1] ) | (*new_dest_address & ~masks[leftover_bits-1]);
void memcpy(char *src, char *dst, size_t nbit){
while( nbit / 8 > 0 ){
*dst++ = *src++; // Multiples of bytes, so copy byte level
nbit -= 8; // Decrement 1 byte
}
// Take care of the remaining bits
*dst = (*src) & (-1 >>(8-nbit));// Do a bitwise and of the src and left bits
}
*dst = (*src) & (-1 >>(8-nbit)); should be rewritten as *dst = (*src) & (-1 <<(8-nbit));
how to check the "dst" do not locate at the end of memory? or if "dst" locates at somewhere that should be protect?
divide by 8 ..store in bytes_copied
- bbarodia March 07, 2013mod by 8 ...store in leftover_bits
perform normal memcpy using bytes at dest_address
new_dest_address = dest_address + bytes_copied ;
new_src_Address = src_address + bytes_copied ;
uint8 mask = (1 << (leftover_bits + 1) - 1;
uint8 maskComplement = ~(mask);
new_dest_address = (*new_src_address & mask ) | (*new_dest_address & maskComplement);