[Func] IsEvenNumber

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

[Func] IsEvenNumber

19 Apr 2021, 00:02

Not much to say. I needed this function in a script and didn't know of a way to do it. Keep in mind that I knew the function would only be receiving whole numbers.

This is what I came up with. :lol: :roll:
I love AHK!!! :bravo:

Code: Select all

IsEvenNumber(int) {
	if (InStr(int/2, ".5"))
		return 0
	
	return 1
}
Is there a built in way to do this in ahk?
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: [Func] IsEvenNumber

19 Apr 2021, 00:06

I would recommend to rather use modulo for this task (with divisor 2): see mod() - it would be a more robust approach.
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: [Func] IsEvenNumber

19 Apr 2021, 00:52

So...

Code: Select all

IsEvenNumber(int) {
	if (Mod(int, 2) = 0)
		return 1

	return 0
}
Thanks for the tip
I just thought the other way was kind of humorous.
digidings
Posts: 24
Joined: 22 Jan 2018, 17:04

Re: [Func] IsEvenNumber

19 Apr 2021, 06:03

if your variable 'int' contains pure integer, you may use the builtin binary '&' operator to check wether 'int' is uneven (and negate the result to check if 'int' is even) like this:

Code: Select all

if ! (int & 1) ; 'int' is even
or with this function:

Code: Select all

IsEvenNumber(int) { ; works with pure integers only
	return (int & 1) ? 0 : 1

    ; Test LSB (least significant bit) to check if int is uneven.
    ;   return 0 if bit is set
    ;   return 1 if bit is not set
    ;
    ;  int           MSB    LSB
    ;  |             V      v
    ; -2 (decimal) = 11111110 (binary)
    ; -1 (decimal) = 11111111 (binary)
    ;  0 (decimal) = 00000000 (binary)
    ;  1 (decimal) = 00000001 (binary)
    ;  2 (decimal) = 00000010 (binary)
    ;  3 (decimal) = 00000011 (binary)
    ;  4 (decimal) = 00000100 (binary)
    ;  5 (decimal) = 00000101 (binary)
    ;  ...
}
test the function and the inline code:

Code: Select all

TestSection:
    loop 5 {
        if (x := IsEvenNumber(A_Index))
            MsgBox ,,call IsEvenNumber(%A_Index%), Result: %x% -> %A_Index% is even
        else
            MsgBox ,,call IsEvenNumber(%A_Index%), Result: %x% -> %A_Index% is not even

        if (x := IsEvenNumber(-A_Index))
            MsgBox ,,call IsEvenNumber(-%A_Index%), Result: %x% -> -%A_Index% is even
        else
            MsgBox ,,call IsEvenNumber(-%A_Index%), Result: %x% -> -%A_Index% is not even
    }
    ; if speed is desired, no need to call a function: use LSB-Test inline:
    loop 5 {
        if A_Index & 1
            MsgBox ,,inline test LSB (%A_Index% & 1), Result: %A_Index% is not even
        else
            MsgBox ,,inline test LSB (%A_Index% & 1), Result: %A_Index% is even
    }
   
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Re: [Func] IsEvenNumber

19 Apr 2021, 09:22

Nice mini binary tutorial.
neogna2
Posts: 589
Joined: 15 Sep 2016, 15:44

Re: [Func] IsEvenNumber

19 Apr 2021, 12:19

digidings wrote:
19 Apr 2021, 06:03
if your variable 'int' contains pure integer, you may use the builtin binary '&' operator to check wether 'int' is uneven (and negate the result to check if 'int' is even)
Nice. Your explanation could be enhanced for those new to bitwise operations by also showing some examples of the operation in table form.

Code: Select all

; 4 in binary:  100
; 1 in binary:  001
; 		         =
; bitwise AND:  000

; 9 in binary:  1001
; 1 in binary:  0001
; 		         =
; bitwise AND:  0001
We could also use bitwise OR or XOR to check evenness/oddness. JNizM's AHK bitwise operations tutorial is good.
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: [Func] IsEvenNumber

19 Apr 2021, 22:17

neogna2 wrote:
19 Apr 2021, 12:19

Code: Select all

; 4 in binary:  100
; 1 in binary:  001
; 		         =
; bitwise AND:  000

; 9 in binary:  1001
; 1 in binary:  0001
; 		         =
; bitwise AND:  0001
This has always been something I've tried to avoid. I've also never tried to learn it... I'm sure it would help me greatly especially when dealing with CLR. I always make wrapper classes because I do not know how to do the bitwise maths or where to start really. I had issues with this early in the .Net Framework thread.

So if they are different they always default to 0?

0101 & 0011 = 0001?
neogna2
Posts: 589
Joined: 15 Sep 2016, 15:44

Re: [Func] IsEvenNumber

20 Apr 2021, 04:59

dd900 wrote:
19 Apr 2021, 22:17
0101 & 0011 = 0001?

Code: Select all

MsgBox % 5 & 3   ; output: 1
;bin 0101 = dec 5
;bin 0011 = dec 3
;       &   
;bin 0001 = dec 1
dd900 wrote:
19 Apr 2021, 22:17
So if they are different they always default to 0?
Yes, but not because the bits are different but because they are not both 1. In other words for each pair of corresponding bits the bitwise AND (&) operation outputs 1 if both operands are 1 and otherwise (in the other three possible cases) outputs 0.
A bitwise AND is a binary operation that takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits
https://en.wikipedia.org/wiki/Bitwise_operation#AND
https://en.wikipedia.org/wiki/Logical_conjunction

The basics of bitwise AND/OR/XOR operations is straightforward. But I don't know enough to try to say anything about more complex bitwise and binary math stuff, where how values are represented (signed integer format) matters. There are plenty of online bitwise and binary math tutorials for specific programming languages and for computer science in general though. For AutoHotkey this jeeswg post is relevant.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Epoch and 51 guests