I just discovered a bug in my code when multiplying hex integers (ex. 0xFFFF) with dec integers (ex. 2). This is the code where the problem occurs:
print_int_new_line(0xFFFF*2);
print_int_new_line(65535*2);
Executing this code gives me the following result:
65534
131070
This is relevant UART code:
void print_int_new_line(uint64_t data) {
print_int(data);
print_new_line();
}
void print_int(uint64_t data) {
char data_buffer[(int)(log(data)+2)]; // size of the number (log(number)+1) + 1 for the /0
ultoa(data, data_buffer, 10); // convert the int to string, base 10
// print the newly created string
print_string(data_buffer);
}
void print_string(char * data) {
// transmit the data char by char
for(; *data != '\0'; data++){
USART_transmit(data);
}
}
void USART_transmit(const char * data){
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = *data;
}
Some info about my setup:
MCU: ATmega2560 Board: Arduino Mega2560 UART baudrate: 38400 IDE: Atmel Studio 7.0.4.1417
Using the AVR toolchain.
I read on this stackoverflow page that multiplication is possible between hex and dec ints. Also, testing this in an online c compiler gives the correct output.
Can anyone give me an explanation?
logis a natural logarithm. Not speaking of the peculiarity of the whole method... - Eugene Sh.inton this platform (AVR toolchain), and for along? Is it really a 8 bits CPU ? You should try to appenduLat the end of the constant - benjarobin