MATLAB: How to convert a negative integer to a binary string, in other words, how can I find Two’s Complement in MATLAB

2binaryconvertdec2binintegerMATLABnegative

DEC2BIN converts non-negative decimal integers to a binary string. I want to create binary strings, with a leading sign bit, from any (negative or positive) integer.

Best Answer

This can be accomplished using the TYPECAST function. For example, to find a binary string for 'n' with respect to 8-bit two's complement, you can use the command,
dec2bin(typecast(int8(n),'uint8'))