An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
December 15, 2021 12:23 pm GMT
Original Link: https://dev.to/govvj/int-to-double-type-casting-27id
int to double type casting
Hi all,
Say you have a 4 byte integer and now you want to convert it to double. You might say that's easy, just add couple of zeros to the front. It's not that simple. Its only work for positive integers.
For simplicity lets assume you have 4 bit integer and you want to convert it to 8 bit.
Let a = -3 = (1101) -> 2's complement
1101 -> 0010 (1s complement) + 1 -> 0011 -> 3
Now if you just add zeros then it will become a = (00001101) = 13
So instead you take the most significant digit (i.e. 4th bit) and paste it to the new bits.
a = (11111101) = (00000010 + 1) = (00000011) = 3 -> -3
Note: when a = 13 we didn't do 2's complement because right most bit was zero.
Original Link: https://dev.to/govvj/int-to-double-type-casting-27id
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To