Jump to content

regular Or and And operator?


Recommended Posts

You mean to do bitwise operation?

No, let me see.....

Okay in c, c++ or c# you have || and | for OR right?

Here is a code in c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            aMsgBox test = new aMsgBox();
            if (true | test.call("Called")) { }
            if (true || test.call("Not Called")) { }
        }
    }
}

class aMsgBox {
    public bool call(String what) {
        MessageBox.Show(what);
        return true;
    }
}

See that the one with | the second condition is called because it check all condition.

But in the second one, it does not called because it's using the same skip tech as Autoit.

Link to comment
Share on other sites

Why though?

Are you performing an action aside from evaluating a logical expression on the second (or onwards) condition? If so, just do it outside the conditional expression.

If it's really necessary. You can just do two consecutive singular logical operations then the combinatorial short-circuited one after. Slow, but it works.

Edited by omikron48
Link to comment
Share on other sites

Why though?

Are you doing an action aside from evaluating a logical expression on the second (or onwards) condition? If so, just do it outside the conditional expression.

If it's really necessary. You can just do two consecutive singular logical operations then the combinatorial short-circuited one after. Slow, but it works.

You said it yourself, slow. I need to it best fast(for end-user debugging and don't want them to feel slow). It is called in a very tight loop. So I'm fitting the most I can get in one line.

Link to comment
Share on other sites

I came across a script that needs a regular OR operator and not a Conditional one.

Is there a way to use the regular one?

if (test() or test2()) ....

Try this.

;
If (test() <> 0 + test2() <> 0 ) Then MsgBox(0, "", " Passed")

Func test()
    ConsoleWrite("test() " & @CRLF)
    Return  -11 ; 0 ;
EndFunc   ;==>test

Func test2()
    ConsoleWrite("test2() " & @CRLF)
    Return  11 ; 0 ;
EndFunc   ;==>test2
;

If (test()* test2()) Then MsgBox(0, "", "AND Passed")

Edited by Malkey
Link to comment
Share on other sites

Try this.

;
If (test() <> 0 + test2() <> 0 ) Then MsgBox(0, "", " Passed")

Func test()
    ConsoleWrite("test() " & @CRLF)
    Return  -11 ; 0 ;
EndFunc   ;==>test

Func test2()
    ConsoleWrite("test2() " & @CRLF)
    Return  11 ; 0 ;
EndFunc   ;==>test2
;

If (test()* test2()) Then MsgBox(0, "", "AND Passed")

Nice thinking, thanks!
Link to comment
Share on other sites

Isn't one pipe the symbol for a bitwise OR operation in c++?

So if (true|Func("test")) ... performs a bitwise OR and then checks the result for true.

In AutoIt you can achieve a similar result with an addition (+) or BitOr, whereas + should be faster i think >_<

So you can write

If (test() <> 0 + test2() <> 0 ) Then ; like Malkey proposed
If BitOr(test(),test2()) Then ; BitOr, slow in AutoIt
If (test()+ test2()) Then ; normal addition, preferred, i think
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Isn't one pipe the symbol for a bitwise OR operation in c++?

So if (true|Func("test")) ... performs a bitwise OR and then checks the result for true.

In AutoIt you can achieve a similar result with an addition (+) or BitOr, whereas + should be faster i think >_<

So you can write

If (test() <> 0 + test2() <> 0 ) Then ; like Malkey proposed
If BitOr(test(),test2()) Then ; BitOr, slow in AutoIt
If (test()+ test2()) Then ; normal addition, preferred, i think

If the result of the comparison is just to be tested for true or false then I don't see it matters whether you use a bitwise or a logical operator.

Wouldn't just

If test(0 or test2(0) then

do the job and be more obvious.

I tried this in C

main()
{
int true;
int nn,yes;
char dum[20];
true = 1 > 0;
res = 0;

for (nn = 1;nn<10;nn++)
if (true|nn)
  yes++;

sprintf(dum,"res = %d",yes);
  puts(dum); //<-------------------shows 9


yes = 0;
for (nn = 1;nn<10;nn++)
if (true|| nn)
  yes++;

sprintf(dum,"Yes = %d",yes);
  puts(dum);//<---------------------shows 9

}

I also tried this

$t = timerinit()

for $n = 1 to 2000
    for $g = 0 to 1000
    if $n <> 0 and $g <> 0 then
        $q = 5
    EndIf
Next
Next

ConsoleWrite("A= " &timerdiff($t) & @CRLF)

$t = timerinit()

for $n = 1 to 2000
    for $g = 0 to 1000
    if $n or $g then
        $q = 5
    EndIf
Next
Next

ConsoleWrite("B= " &timerdiff($t) & @CRLF)

$t = timerinit()

for $n = 1 to 2000
    for $g = 0 to 1000
    if BitOr($n,$g) then
        $q = 5
    EndIf
Next
Next

ConsoleWrite("C= " & timerdiff($t) & @CRLF)

for $n = 1 to 2000
    for $g = 0 to 1000
    if $n + $g then
        $q = 5
    EndIf
Next
Next

ConsoleWrite("D= " & timerdiff($t) & @CRLF)

for $n = 1 to 2000
    for $g = 0 to 1000
    if $n *$g then
        $q = 5
    EndIf
Next
Next

ConsoleWrite("E= " & timerdiff($t) & @CRLF)

and got this

A= 3208.19210262757<-------------"<> 0 And <> 0" is slow

B= 2039.39076055756<------------simple Or is fastest

C= 3513.50464933392<-----------BitOr is slowest

D= 2346.2825582581<---------- addition is slower than Or

E= 2351.23011444192<---------- multiplication same as addition

EDIT: Corrected, see next post

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I would like to point out in your code that C and D don't re-initialize the timer.

Thanks,

Jarvis

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

I would like to point out in your code that C and D don't re-initialize the timer.

Thanks,

Jarvis

Oops:>.

Thanks Jarvis, now corrected.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

If the result of the comparison is just to be tested for true or false then I don't see it matters whether you use a bitwise or a logical operator.

Wouldn't just

If test(0 or test2(0) then

do the job and be more obvious.

Here is the difference, and the reason why the thread-opener wanted this.

; first test: Or-Operator:
$string = "first test: Or" & @CRLF & "-----------------" & @CRLF
If _TestOne($string) Or _TestTwo($string) Then
    $string &= "__________________" & @CRLF & "Result: true"
EndIf

MsgBox(0, '', $string)

; second check addition: 
$string = "second test: Addition" & @CRLF & "-----------------" & @CRLF
If _TestOne($string) + _TestTwo($string) Then
    $string &= "__________________" & @CRLF & "Result: true"
EndIf

MsgBox(0, '', $string)

Func _TestOne(ByRef $teststring)
    $teststring &= "_TestOne called" & @CRLF
    Return True
EndFunc

Func _TestTwo(ByRef $teststring)
    $teststring &= "_TestTwo called" & @CRLF
    Return True
EndFunc

As you can see, the first test with or only runs the first function, but if you always want both to run, you have to use a bitwise or an arithmetic operation.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Here is the difference, and the reason why the thread-opener wanted this.

; first test: Or-Operator:
$string = "first test: Or" & @CRLF & "-----------------" & @CRLF
If _TestOne($string) Or _TestTwo($string) Then
    $string &= "__________________" & @CRLF & "Result: true"
EndIf

MsgBox(0, '', $string)

; second check addition: 
$string = "second test: Addition" & @CRLF & "-----------------" & @CRLF
If _TestOne($string) + _TestTwo($string) Then
    $string &= "__________________" & @CRLF & "Result: true"
EndIf

MsgBox(0, '', $string)

Func _TestOne(ByRef $teststring)
    $teststring &= "_TestOne called" & @CRLF
    Return True
EndFunc

Func _TestTwo(ByRef $teststring)
    $teststring &= "_TestTwo called" & @CRLF
    Return True
EndFunc

As you can see, the first test with or only runs the first function, but if you always want both to run, you have to use a bitwise or an arithmetic operation.

I see, thanks. I hadn't realised it was required that both functions were run.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...