Bit pattern problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Bit pattern problem

https://code.sololearn.com/cOhF9EhdR6HV/?ref=app The output of ~a and bit pattern of ~a doesn't match, why? And why it's negative always?

18th Mar 2021, 3:08 PM
Kashyap Kumar
Kashyap Kumar - avatar
1 Answer
+ 2
sizeof give you size in bytes, while bit pattern should use number of bits (1 byte == 8 bits)... do your bit pattern truncate your binary value to 4 bits, while there's 32 bits ^^ result of 1-complement (~) is negative because the value from wich you get complement is positive... and int are signed values... 1-complement inverse all bits 2-complement is 1-complement + 1 later is how are handled signed binary (left most bit determine sign, other bits determine value in a way that you get corrects mathematic result by using those values)... https://www.geeksforgeeks.org/1s-2s-complement-binary-number/
18th Mar 2021, 3:39 PM
visph
visph - avatar