Pergunta de entrevista da empresa L3Harris

countingBits problem: given a number, write C code that detects how many bits would be set in the binary representation of the number What are the issues with the below code: int calcSquare(volatile int* num){ return *num * *num; }

Resposta da entrevista

Sigiloso

23 de set. de 2025

int calcSquare(voltatile int*num) { return *num * *num; } first one is, volatile means compiler is not optimize access to the value *num. Generally volatile is used for hardware access like status registers, shared memory where the value can change unexpectedly. Second one is, before returning the value, it reads twice which could yield different values for every read. This leads to unexpected results.