Two's complement: Difference between revisions

No edit summary
Line 310:
Module Complement2{
// we use binary.and to get a number in range of byte 0 to 255
byte k=100, z=binary.and(binary.not(k)+1, 0xFF)v
v=random(0, 255)
print z=156 // z is type of byte always positive
k=v
// sint() get an unsigned value and return a signed one with same bits
z=binary.and(binary.not(k)+1, 0xFF)
print sint(z+0xFFFFFF00)=-100 // using 4bytes, we add unsinged 0xFFFFFF00
print v
print sint(z,1)=-100 // using only one byte
print z
print z=156 255-v+1 // z is type of byte always positive
print sint(z+0xFFFFFF00)=-100v // using 4bytes, we add unsinged 0xFFFFFF00
}
Complement2
Complement2
</syntaxhighlight>
{{out}}
<pre>
2
254
True
True
208
48
True
True
404

edits