RosettaCodeData/Task/Bitwise-operations/M2000-Interpreter/bitwise-operations.m2000

44 lines
1.6 KiB
Plaintext

module binary_ops{
select case random(1, 6)
case 1
Double x=10, y=2
case 2
Decimal x=10, y=2
case 3
Integer x=10, y=2
case 4
Long x=10, y=2
case 5 ' byte from 0 to 255 (unsigned)
Byte x=10, y=2
case else
Long Long x=10, y=2
end select
print type$(x)
//x & y values from 0 to 4294967295
print binary.not(x)=4294967285, sint(binary.not(x))=-11
print binary.and(x, y)=2
print binary.or(x, y)=10
// y values -31 to 31
print binary.xor(x, y)=8
print binary.shift(x, y)=40
print binary.shift(x, -y)=2
print binary.rotate(x, y)=40
print binary.rotate(x, -y)=2147483650, sint(binary.rotate(x, -y))=-2147483646
// Binary.Neg return Unsigned from signed values: -2147483648 (0x8000_0000&) to 2147483647 (0x7FFF_FFFF&)
// values>2147483647 return 2147483648
// values <-2147483648 return 2147483647
// 0xFFFF_ABCD& (signed value), 0xFFFF_ABCD unsinged value same bits
print 0xFFFF_ABCD&=-21555, 0xFFFF_ABCD=4294945741, sint(0xFFFF_ABCD)=-21555
print Binary.Neg(0xFFFF_ABCD&)=21554, Binary.Not(0xFFFF_ABCD)=21554
print sint(Binary.Neg(0xFFFF_ABCD&)+ uint(0xFFFF_ABCD&))=sint(Binary.Neg(0))
// signed 0xFFFF_ABCD& has same bits as unsigned 0xFFFF_ABCD,
// but have different value as used in calculations
print Binary.Neg(0xFFFF_ABCD&)=Binary.Not(0xFFFF_ABCD), 0xFFFF_ABCD&<>0xFFFF_ABCD
print Binary.Neg(0x0FFF_ABCD&)=Binary.Not(0x0FFF_ABCD), 0xFFFF_ABCD&<>0xFFFF_ABCD
// Add (modulo 32), x unsigned and add a negate signed (converted to unsigned)
print binary.add(x, binary.neg(y), 1)=8 ' 10 - 2 =12
print binary.add(x, binary.neg(-y), 1)=12 ' 10 - - 2 = 12
print sint(binary.neg(x))=-11, binary.neg(x)=binary.not(x)
}
binary_ops