Reputation: 1135
I was wondering how do you convert from an 8 bit 2's complement to a 16 bit 2's complement signed number?
1100 0110 is an example
Upvotes: 5
Views: 22278
Reputation: 1
The way I believe you would do it if you wanted a direct conversion is put however many extra bits you need as zeros. Like 11000110 as 8-bit would be 0000000011000110 in 16 bit since it's the same number in both formats. depending on the use case this may give undesirable results. if converting a number that needs minimum and maximum values, like and audio file or an image, then you might want to use multiplication to find the correct answer. Since the maximum value of a 16 bit integer is 256 times larger than that of an 8 bit integer you would multiply the number by 256 (0000000011111111 in 16 bit). Doing that method leaves us with a value of 1100010100111010 using a binary calculator (calculator: https://www.rapidtables.com/calc/math/binary-calculator.html)
Edit: I noticed that your were talking about signed integers so I don't know if it would be a different process or not.
Upvotes: 0
Reputation: 590
It's called sign extending. You simply repeat the most significant bit until you have the right number of bits. For your example, it would be 1111 1111 1100 0110
Upvotes: 10