unsigned8 bit operations

Discussions related to Visual Prolog
Loffy
Posts: 22
Joined: 15 Aug 2019 11:32

unsigned8 bit operations

Unread post by Loffy » 7 Sep 2019 13:48

I perform a ByteOut = bit::bitXor(Byte1, Byte2) operation where both Byte1 and Byte2 are defined as unsigned8.

The resulting ByteOut value is an unsigned32 value.

Is there a better way to make ByteOut an unsigned8 other than using bit::setHigh / setLow predicates to convert between unsigned32 and unsigned8 values when using the bit class operations?

Regards,

Loffy

Martin Meyer
VIP Member
Posts: 283
Joined: 14 Nov 2002 0:01

Re: unsigned8 bit operations

Unread post by Martin Meyer » 7 Sep 2019 17:28

Yes, there is a better way. Just make sure that ByteOut has domain unsigned8:

Code: Select all

    open core   clauses     run() :-         hasDomain(unsigned8, Byte1),         hasDomain(unsigned8, Byte2),         Byte1 = 1 * 0x04 + 0 * 0x02 + 1 * 0x01,         Byte2 = 1 * 0x04 + 1 * 0x02 + 0 * 0x01,         hasDomain(unsigned8, ByteOut),         ByteOut = Byte1 ^^ Byte2,         stdIO::writeF('%02x', ByteOut).
Or, without hasDomain/2, by a predicate declaration:

Code: Select all

    open core   class predicates     myXor : (unsigned8 Byte1, unsigned8 Byte2) -> unsigned8 ByteOut. clauses     myXor(Byte1, Byte2) = Byte1 ^^ Byte2.   clauses     run() :-         Byte1 = 1 * 0x04 + 0 * 0x02 + 1 * 0x01,         Byte2 = 1 * 0x04 + 1 * 0x02 + 0 * 0x01,         ByteOut = myXor(Byte1, Byte2),         stdIO::writeF('%02x', ByteOut).
Also check Bitwise and boolean operators.
Regards Martin

Loffy
Posts: 22
Joined: 15 Aug 2019 11:32

Re: unsigned8 bit operations

Unread post by Loffy » 8 Sep 2019 5:09

Martin,

Thanks.

Loffy

User avatar
Thomas Linder Puls
VIP Member
Posts: 2438
Joined: 28 Feb 2000 0:01

Re: unsigned8 bit operations

Unread post by Thomas Linder Puls » 9 Sep 2019 8:29

This works fine:

Code: Select all

BX = hasDomain(unsigned8, B1 ^^ B2),
Regards Thomas Linder Puls
PDC

Post Reply