Bitcmp - differences between uint8 and int8 assumedtypes
Afficher commentaires plus anciens
Hi,
I am using the function bitcmp for a school project and I came across a strange behaviour of this function, shown below:
>> a = bitcmp(53, 'int8')
a =
-54
>> b = bitcmp(53, 'uint8')
b =
202
Assuming the decimal defined in this example is well within the 8 bit representation (both signed and unsigned ranges), why is the outcome different? I was expecting it to be exactly the same.
Do you know which type of operation is bitcmp performing for uint8 and int8 types? I would like to do the same procedure "manually" to better understand this function.
Thanks
Réponses (1)
bitxor(int8(53), int8(-1))
bitxor(uint8(53), uint8(255))
typecast(int8(-54), 'uint8')
You should not be expecting the same result for the two. If you are expecting -54 for both, then you have the problem that negative values are out of range for uint8. If you are expecting 202 for both, then you have the problem that values > 127 are out of range for int8.
If you are using signed integers, then bitwise complement of a positive number is always negative, because the sign bit gets flipped. If you are using signed integers, then bitwise completement of a negative number is positive unless the number is the one with all bits set (in which case the result is 0), because the sign bit gets flipped.
If you are using unsigned integers, then bitwise complement of a number less than half of the maximum is always greater than half of the maximum, because the most significant bit gets flipped. If you are using unsigned integers, then bitwise complement of a number greater than half of the maximum is always less than half of the maximum, because the most significant bit gets flipped.
6 commentaires
Catarina Carvalho
le 22 Nov 2020
202 is outside the range of values int8 can represent. The minimum value it can represent is:
intmin('int8')
and the maximum is:
intmax('int8')
Similarly, if you're working with uint8 data you can store numbers in the range:
boundsOfType = @(x) [intmin(x) intmax(x)];
boundsOfType('uint8')
Catarina Carvalho
le 22 Nov 2020
Bruno Luong
le 22 Nov 2020
The bit representation of int8(53) is
>> dec2bin(53,8)
ans =
'00110101'
The 2-complement is '11001010'
>> char((1-(dec2bin(53,8)-'0'))+'0')
ans =
'11001010'
This is the binary representation of int8(-54) or uint8(202). They are identical in binary since (202=2^8-54)
>> u=typecast(int8(-54),'uint8')
u =
uint8
202
>> dec2bin(u)
ans =
'11001010'
>> dec2bin(int8(-54))
ans =
'11001010'
Look at the binary representation.
dec2bin(int8(53), 8)
The complement of that is 11001010. If we use that bit pattern to create a signed 8-bit integer:
x = 0b11001010s8
x = 0:10;
[x; bitcmp(int8(x))]
Notice the pattern: for values up to 127, bitcmp(x) is -x-1 . Or to look at it another way, the sum of x and bitcmp(x) is -1 .
00110101
11001010
--------
11111111
In every case with bitcmp, 0 is replaced by 1 and 1 is replaced by 0, so the sum of the value and its bitwise complement will have 1 in every place -- either it was originally 0 and the complement was 1, or else it was originally 1 and the complement was 0. It is never possible to have a carry when you add together a value and its bitwise complement, and its is never possible to end up with a 0 in any position.
You can also think of this as starting with the all-1 value, and subtracting the bitwise representation of the original value to get the bit representation of the new value.
Catégories
En savoir plus sur Just for fun dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!