Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting int to char C formula

Tags:

c

char

int

modulo

int num = 65537;
char p = (char)num; //char = 1;

Whats going on here? Is it p=num%(127+128)-1 or p=num%256 or something else? I need to know why p is equal to 1. thanks!

like image 566
Xiobiq Avatar asked Dec 09 '25 05:12

Xiobiq


1 Answers

since 65537 is 00000000 00000001 00000000 00000001 in binary, but the char type has only 1 byte, the last byte is considered for the char value, wich is 00000001 = 1

like image 105
ksmonkey123 Avatar answered Dec 10 '25 21:12

ksmonkey123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!