Question Details

No question body available.

Tags

c gameboy gbdk

Answers (2)

February 15, 2026 Score: 2 Rep: 13,318 Quality: Medium Completeness: 60%

#include

#define UINT8 unsigned char

UINT8 setNibble(UINT8 orig, UINT8 value) { UINT8 rtn = 0; rtn = (orig & 0b11110000) | (value & 0b00001111); //doesn't work, returns 0 //rtn = (64 & 240) | (2 & 15); //works, basic values, returns a 66 //rtn = (64 & 240) | (value & 15); //works, without orig, returns a 66 //rtn = (orig & 240) | (value & 15); //doesn't work, returns 64 //UINT8 mask = (UINT8)0xF0; //solution suggested by Stackoverflow AI //UINT8 low = (UINT8)(value & 0x0F); //rtn = (UINT8)((orig & mask) | low); //doesn't work, returns a 2 return rtn; } int main() { UINT8 num = setNibble(65,2);

printf("ORIG %02x\n", 65); printf("VALU %02x\n", 2); printf("RES %02x\n", num);

return 0; }

generates what is expected

ORIG 41 VALU 02 RES 42
February 16, 2026 Score: -1 Rep: 13,198 Quality: Low Completeness: 40%

If you want both numbers to be under 0x10 (15 or less) then you have to shift one of them, as in

unsigned set_nibble(unsigned upper, unsigned lower) { return ((upper & 0x0f)