Ooops
November 29, 2020, 07:50:21 PM

### Author Topic: determine if an integer value is odd or is even  (Read 4728 times)

#### RemiD

• Hero Member
• Posts: 1077
##### determine if an integer value is odd or is even
« on: October 21, 2019, 10:03:41 PM »
needed this today, and i knew that i had the functions on my harddrive, somewhere :
Code: [Select]
`;determine if an integer value is odd or is even 20160101Graphics3D(640,480,32,2)SeedRnd(MilliSecs())For i% = 0 To 101 R1% = IsOdd(i) R2% = IsEven(i) DebugLog("i = "+i) DebugLog("IsOdd ? "+R1) DebugLog("IsEven ? "+R2) WaitKey()NextEnd()Function IsOdd(TInt%) R% = Abs(TInt) Mod 2 If(R = 0)  Return False ElseIf(R = 1)  Return True EndIfEnd FunctionFunction IsEven(TInt%) R% = Abs(TInt) Mod 2 If(R = 0)  Return True ElseIf(R = 1)  Return False EndIfEnd Function`
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### Steve Elliott

• Hero Member
• Posts: 2862
• elgol 2021
##### Re: determine if an integer value is odd or is even
« Reply #1 on: October 21, 2019, 10:09:45 PM »
I always think it's tidier/less code to not use else in cases when there are only 2 possibilities.

Code: [Select]
`Function IsOdd(TInt%) R% = Abs(TInt) Mod 2 If(R = 0)  Return False Endif Return TrueEnd Function`
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry Pi 400, Pi4, C64, ZX Spectrum Next.

#### STEVIE G

• Sr. Member
• Posts: 498
##### Re: determine if an integer is odd or even
« Reply #2 on: October 21, 2019, 10:10:12 PM »
Why not use one simple function ... if it isn't even it's odd.

Code: [Select]
`Function IsEven( I% ) Return ( Abs(I) Mod 2 ) = 0End Function`

#### Steve Elliott

• Hero Member
• Posts: 2862
• elgol 2021
##### Re: determine if an integer value is odd or is even
« Reply #3 on: October 21, 2019, 10:30:45 PM »
Yes you could re-write it completely, simplify even more and use a not if required.

Code: [Select]
`If( Not IsEven( num ) )  ; odd number`
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry Pi 400, Pi4, C64, ZX Spectrum Next.

• Hero Member
• Posts: 517
##### Re: determine if an integer value is odd or is even
« Reply #4 on: October 21, 2019, 11:26:34 PM »
If number & 1
Number is odd
Else
Number is even
------------------------------------------------
8 rabbits equals 1 rabbyte.

#### STEVIE G

• Sr. Member
• Posts: 498
##### Re: determine if an integer value is odd or is even
« Reply #5 on: October 22, 2019, 12:50:29 AM »
If number & 1
Number is odd
Else
Number is even

I knew there must be a bit shift variant.

#### Steve Elliott

• Hero Member
• Posts: 2862
• elgol 2021
##### Re: determine if an integer value is odd or is even
« Reply #6 on: October 22, 2019, 08:00:10 AM »
Haha, nice one.  I love these little efficiencies.
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry Pi 400, Pi4, C64, ZX Spectrum Next.

#### RemiD

• Hero Member
• Posts: 1077
##### Re: determine if an integer value is odd or is even
« Reply #7 on: October 22, 2019, 12:06:31 PM »
i think that you should spend less time trying to optimize a procedure which is already fast enough (it takes 0.00012 millisecond so 0.12 microsecond to complete the procedure on my 8 years old, low end, laptop)
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### Steve Elliott

• Hero Member
• Posts: 2862
• elgol 2021
##### Re: determine if an integer value is odd or is even
« Reply #8 on: October 22, 2019, 12:13:04 PM »
a) I think you should be grateful for some neat advice and not instead sulk.  And b) that's a lazy attitude to coding, these little efficiencies all add-up.  No excuse for ugly bloaty routines when you were given some good advice, and like you mentioned you can store and re-use them again.

Thanks guys, I for one enjoyed your input.
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry Pi 400, Pi4, C64, ZX Spectrum Next.

#### GfK

• Full Member
• Posts: 161
##### Re: determine if an integer value is odd or is even
« Reply #9 on: October 22, 2019, 01:54:29 PM »
Code: [Select]
`Function IsOdd(num%)  Return (num & 1)End Function`
Code: [Select]
`Function IsEven(num%)  Return Not (num & 1)End Function`
...or something like that.  Don't use Blitzwhateverstrainthisis any more.
Intel I9-9900K 3.6-5.0GHz | GeForce RTX2070 8GB | 32GB RAM | 500GB NVMe M.2 SSD | 1TB HDD | Windows 10 x64.
MSI Apache Pro | I7-7700HQ | GeForce GTX1060 3GB | 8GB RAM | 128GB SSD | 1TB HDD | Windows 10 x64.

#### RemiD

• Hero Member
• Posts: 1077
##### Re: determine if an integer value is odd or is even
« Reply #10 on: October 22, 2019, 05:00:58 PM »
thanks for teaching me how to behave and programming "Steve Elliott", i feel better now. i am really grateful to you.
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### Steve Elliott

• Hero Member
• Posts: 2862
• elgol 2021
##### Re: determine if an integer value is odd or is even
« Reply #11 on: October 22, 2019, 08:51:52 PM »
lol personally I love to learn.  If somebody comes up with a better solution than myself I love that!  Because I've learnt something extra...Rather than tell people they are wasting their time and now come up with a sarcastic remark.
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry Pi 400, Pi4, C64, ZX Spectrum Next.

#### RemiD

• Hero Member
• Posts: 1077
##### Re: determine if an integer value is odd or is even
« Reply #12 on: October 23, 2019, 06:11:19 PM »
@steve elliott>>you are too smart for me, i give up
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

• Hero Member
• Posts: 517
##### Re: determine if an integer value is odd or is even
« Reply #13 on: October 23, 2019, 09:24:04 PM »

Code: [Select]
`Function isEven(num%) If num Shr 1 Shl 1 = num Then Return True Return FalseEnd FunctionFunction isOdd(num%) If num Shr 1 Shl 1 <> num Then Return True Return FalseEnd FunctionFor i = 0 To 100 If isEven(i) Print i+" is even" ElseIf isOdd(i) Print i+" is odd" Else Print i+" is nothing" EndIfNext`
Code: [Select]
`Function isEven(num%) If ((num - 1) Xor num) <> 1 Then Return True Return FalseEnd FunctionFunction isOdd(num%) If ((num - 1) Xor num) = 1 Then Return True Return FalseEnd Function`
Code: [Select]
`Function isEven(num%) For i = -2147483648 To +2147483647 Step 2 If num = i Then Return True Next Return FalseEnd FunctionFunction isOdd(num%) For i = -2147483647 To +2147483647 Step 2 If num = i Then Return True Next Return FalseEnd Function`
------------------------------------------------
8 rabbits equals 1 rabbyte.

#### Derron

• Hero Member
• Posts: 3265
##### Re: determine if an integer value is odd or is even
« Reply #14 on: October 23, 2019, 09:28:38 PM »
Code: [Select]
`Function isEven(num%) For i = -2147483648 To +2147483647 Step 2 If num = i Then Return True Next Return FalseEnd FunctionFunction isOdd(num%) For i = -2147483647 To +2147483647 Step 2 If num = i Then Return True Next Return FalseEnd Function`

Ohh no ... seems to contain a mistake. Else I would prefer these functions as I see they check all potential candidates. Thanks for the laugh

bye
Ron