The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 bitwise NOT, XOR, NAND ?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

tib

Switzerland
105 Posts

Posted - 09/06/2006 :  12:47:46 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic

Hi,
might be a stupid question... but could not find a help nor forum topic...
AND & and OR | operators exist in Labtalk.
Is there an easy way to realize NOT, XOR and NAND in LabTalk?
e.g.
bin.(dec) --> bin. (dec.)
NOT 1100 (12) --> 0011 (3)
1101 (13) XOR 0100 (4) --> 1011 (11)
...
Thanks for hints,
Tilman.

tib

Switzerland
105 Posts

Posted - 09/07/2006 :  04:28:11 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,
in the meantime I found a possible solution.
Key is the NOT function. NOT can be realized in the following way:

NOT z = MOD(2^n-1-z,2^n)
where
n is the number of digits of the binary number
z is the number you want to apply NOT on

with this you can realize NAND:

x NAND y = NOT (x & y)

with this you can realize XOR:

x XOR y = (x OR y) AND NOT(x AND y)
= (x | y) & (MOD(2^n-1-(x & y),2^n))

Maybe there is even a shorter way...
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 09/07/2006 :  10:25:46 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Tilman,

I question your definition of the bitwise NOT (or complement) operator. My C reference uses all 16 bits of unsigned integer to represent a decimal number in bitwise operations. Therefore...

NOT x = 65535 - x
x NAND y = NOT (x&y) = 65535 - (x&y)
x XOR y = (x|y) & (65535 - (x&y))

In Origin C you can use ^ for XOR by including a pragma in your code. Therefore you can create your own bitwise operators as shown below. Note that bw_xor(x,y) returns the same integer as does my LabTalk XOR expression above.

#pragma xor(push, FALSE)

uint bw_xor(uint x, uint y)
{
return x^y;
}

uint bw_not(uint x)
{
return 65535 - x;
}

uint bw_nand(uint x, uint y)
{
return 65535 - (x&y);
}

Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 09/07/2006 11:37:11 AM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000