Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Bit operations for beginners

by mrborisguy (Hermit)
on Jun 22, 2005 at 00:15 UTC ( [id://468870]=note: print w/replies, xml ) Need Help??


in reply to Bit operations for beginners

I just have to be difficult, it's the way I am:

There are 4 bit operations, and, or, xor and inverse

It's been shown already in this thread, but not said yet that there are actually more than 4 bit operations. 'and', 'or', 'xor', and 'inverse' are the common ones used in Perl (and most of the programming languages that have crossed my path) and in fact any bit operator can be created using those - they're a complete set or something (actually, still complete without 'xor'); but technically they aren't the only four bit operators.

(Good work, by the way!) ++

    -Bryan

Replies are listed 'Best First'.
Re^2: Bit operations for beginners
by ikegami (Patriarch) on Jun 27, 2005 at 23:20 UTC

    For two inputs, there are 16 operations. Most of them are rarely useful, which is why we don't have Perl operators for them.

    By the way, everything can be implemented solely with nand:

    p | q | p nand q ---+---+---------- 0 | 0 | 1 0 | 1 | 1 1 | 0 | 1 1 | 1 | 0 not p === p nand p p and q === (p nand q) nand (p nand q) p or q === (p nand p) nand (q nand q) p xor q === (p nand (q nand q)) nand ((p nand p) nand q)

    Other operatations could also be used instead of nand. nor, for example:

    p | q | p nor q ---+---+--------- 0 | 0 | 1 0 | 1 | 0 1 | 0 | 0 1 | 1 | 0 ~(p nor q) not p === p nor p p and q === (p nor p) nor (q nor q) p or q === (p nor q) nor (p nor q) p xor q === [something very long]
      p xor q === [something very long]

      Hell's teeth! You weren't kidding.

      I remember xor as (p and not q) or (q and not p) and it's been something like 30 years since I played with nand and nor expansions, so I expanded that:

      xor === (p and not q) or (q and not p) expand the 'not's === (p and (q nor q)) or (q and (p nor p)) expand the 'and's === ((p nor p) nor ((q nor q) nor (q nor q))) or ((q nor q) nor ((p nor p) nor (p nor p))) expand the 'or's === (((p nor p) nor ((q nor q) nor (q nor q))) nor ((q nor q) nor ((p nor p) nor (p nor p)))) nor (((q nor q) nor ((p nor p) nor (p nor p))) nor ((p nor p) nor ((q nor q) nor (q nor q))))

      Then it struck me that ((x nor x) nor (x nor x)) is x, which reduces it to

      === (((p nor p) nor q) nor ((q nor q) nor p)) nor (((q nor q) nor p) nor ((p nor p) nor q))

      It's certainly more digestable (just :), but are there any other reductions in there?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

        Yeah, you might enjoy:

        (p nor q) nor ((p nor p) nor (q nor q))

        Remember that nor returns true only if both arguments are false. So, we want to return true if the inputs aren't both false or both true. The former is simply p nor q and the latter is (not p) nor (not q) and we use the expansion of not.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://468870]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-25 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found