Jump to content

Recommended Posts

Posted

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.

Posted (edited)

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
Posted

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.

Posted (edited)

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
Posted

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!
Posted (edited)

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

Posted (edited)

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.
Posted

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.
Posted

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

Posted

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.
Posted (edited)

Trivia:

The technique that makes only one function execute in OP's example is called Lazy Evaluation and if you're bored you can utilize it to create something like this. >_<

Edited by monoceres

Broken link? PM me and I'll send you the file!

Posted (edited)

If both are required to run, but the results are not required, it is really just better to put them on two lines. Has anyone tested that speed?

If the results are required, then all these mathematical operators are rather nonsensical, as the data has now been modified.

Edited by Richard Robertson

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
  • Recently Browsing   0 members

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