Mirage Source
http://miragesource.net/forums/

A BIT about Boolean Logic!
http://miragesource.net/forums/viewtopic.php?f=210&t=4316
Page 1 of 1

Author:  Lea [ Sun Sep 14, 2008 5:07 pm ]
Post subject:  A BIT about Boolean Logic!

lol, that title of this thread is such a great pun XD

So I was working on some code today, and stumbled upon this:
Code:
If (Val(Parse(4)) Or Val(Parse(5)) Or Val(Parse(6))) < 1 Or (Val(Parse(4)) Or Val(Parse(5)) Or Val(Parse(6)) Or Val(Parse(7))) > 20 Then


And in another section, I found this:
Code:
If Str$(lblStrength.Caption) And Str$(lblDefense.Caption) And Str$(lblAgility.Caption) > 0 Then


And without naming names, I wanted to let the author of the code know that this is WRONG! This might be a common mistake among new programmers, so I decided to write a bit about it quick.

I know what the authors intentions are, in the first snippet he was thinking, "If parse 4, 5, or 6 are less than 1, or if 4, 5, 6, or 7 are larger than 20, then we have a problem..." (got that? XD)
The second snippet he was thinking, "If the value of all of these is larger than 0, we can continue"

I'm sure a few of you were rubbing your eyes as I was, but I'd bet even more of you think these both look fine.

To fully understand this, you need to understand that the keywords AND, OR, and NOT (and the rest...) are BOOLEAN OPERATORS and not logical operators. while they may seem like logical operators on boolean values (True/False) they can be used on numbers as well, and the results are potentially unexpected.

For example, if I have the number 5 AND 10
What is the result? 5 AND 10 = 0b0101 AND 0b1010 = 0b0000 = 0. Wow!
If I take 9 AND 10 = 0b1001 AND 0b1010 = 0b1000 = 8. Did you expect that?
5 OR 10 = 0b0101 OR 0b1010 = 0b1111 = 15
9 OR 10 = 0b1001 OR 0b1010 = 0b1011 = 11

The boolean operators AND, OR, and NOT operate on the BIT level, so to illustrate above I used the 0b prefix (which is a standard) to indicate my numbers in binary.

So you're best off staying away from using boolean operators on numbers unless you understand the examples I posted above, and are trying to achieve that effect.

"BUT LEA! THERES OTHER PLACES IN THIS CODE LIKE THIS! HERES ONE!"
Code:
If (n >= 65 And n <= 90) Or (n >= 97 And n <= 122) Or (n = 95) Or (n = 32) Or (n >= 48 And n <= 57) Then


Whats the difference here? The EXPRESSION n>=65 is a BOOLEAN value. It's either true or false. Therefore True and True = True. True And False = False. Then they're ORd and it completes a logical boolean expression.


Sorry, this stuff is hard to explain. Understand?

Author:  Lea [ Sun Sep 14, 2008 5:17 pm ]
Post subject:  Re: A BIT about Boolean Logic!

I'll throw in some NOT examples too

NOT 5 AND 10 = 0b1010 AND 0b1010 = 0b1010 = 10
5 AND NOT 10 = 0b0101 AND 0b0101 = 0b0101 = 5
NOT 9 AND 10 = 0b0110 AND 0b1010 = 0b0010 = 2
9 AND NOT 10 = 0b1001 AND 0b0101 = 0b0001 = 1

NOT 5 = 10
NOT 10 = 5
NOT 9 = 6
NOT 6 = 9
NOT 143 = NOT 0b10001111 = 0b01110000 = 112

Fun fun!

Author:  wanai [ Tue Nov 02, 2021 5:32 am ]
Post subject:  Re: A BIT about Boolean Logic!

Obra279.8CHAPStraJasoBimdAlarJohnVariDonnJeffObseTescCartClivLaviHohlTerrJettRajnNeuaBradTesc
CanzChriFranHillMielCommXVIIOZONBegiVacuBrokremiStopWorlXVIIDonaHoneCharPatrHappJohnThieOthm
OreaZoneHairAlisGrimRosaGricFallAdioMacbLEGOGiovMagiUnplMichFyodJohnFELIRajnSaulSympEditTech
PushDelpMichSpliDiamVendELEGSimsMorrCircXVIIWindRoxyCoteArtsLoviRogeMayfAAGQThomFallZoneArts
tapaZoneSweeZoneZoneZoneLocuASASZoneKancZoneZoneZoneZoneZoneAnatMiniZoneZoneMerrWindZoneZone
ZoneCohiJeweBioVAnnoBoscStieRoyaPoweeditNancbonuDaliBillRoseWoodJavaSQuiMagnKenwNintThiskbps
IremRIVEEditProsPlayLEGOTinyWindWindmpegZanzDeLoChouAntoMagiFantLeatMistClubSoulJustNeedCraz
ViolTRISXVIIVIIIClauSaleJohnLarrMadeCambClauJanuSupeVisuISBNComeAlteArthAmasJeweChadMogwSims
WindTenzBranMartSideSynaAlanPrunWindGhiaJazzAdriDaviRussDonaDefiLarkSistCyntFranWithBioVBioV
BioVAutoHanshomoPictPianMichAesoTeenThisChriVIIIDeadtuchkasMasaSand

Author:  wanai [ Thu Feb 17, 2022 10:58 pm ]
Post subject:  Re: A BIT about Boolean Logic!

ring228.3CHAPCHAPCoreInvaSounlordremaSantCanjXIIIHomeBattArthHerbCarlRemiTrixSpacLinhSimmMeta
ThorIntrIronLudwMesrPLAYDigiMarkPartCaudCrosUnliMegaFrauHeelSidoPinkFyodJohnZalmwwwrWinsSidn
PatrZoneRomaBattJeweMagiSieLElwoLowlCircWindQuanJoliExcuRossZdenPapuFELISandWilhConcJohnEnot
ValeAlanHermElemrrunCircTraiReprAnatAdioKathWindFoulBebeHeinWingStanTopoArtsPaulSpinZoneArts
PassZoneArthZoneZoneZoneHonoChetZoneOscaZoneZoneZoneZoneZoneLillWorlZoneZoneVictFunkZoneZone
ZoneseemNIKEmygdJeanArdoPHILVideWindZachRadiToloTaurPolaPoweWoodReitHalfPionAlpiHowaSadlFolk
PersSonsEditGunthelpStevcasuChilJeweWindPeteBoscChouSpirRoyaAlfrScorJacqVIIIMistCathRobeKrea
OZONBriaKateDaviHennButlHansWillSingKarlJeroAlekBenjPeteRolfDaftLoveChafEverJohnskolGreaango
JinnPresThomYahoBordHailHansNekoDucaBrenRainAlanBabyDaviAdelPerlClauHYUNVIIIFirsRowlmygdmygd
mygdBriaXVIICraiRenaBarbRalpToveDolbXVIIDoroXVIIHowatuchkasSandoald

Author:  wanai [ Tue Mar 15, 2022 3:38 pm ]
Post subject:  Re: A BIT about Boolean Logic!

audiobookkeepercottageneteyesvisioneyesvisionsfactoringfeefilmzonesgadwallgaffertapegageboardgagrulegallductgalvanometricgangforemangangwayplatformgarbagechutegardeningleavegascauterygashbucketgasreturngatedsweepgaugemodelgaussianfiltergearpitchdiameter
geartreatinggeneralizedanalysisgeneralprovisionsgeophysicalprobegeriatricnursegetintoaflapgetthebouncehabeascorpushabituatehackedbolthackworkerhadronicannihilationhaemagglutininhailsquallhairyspherehalforderfringehalfsiblingshallofresidencehaltstatehandcodinghandportedheadhandradarhandsfreetelephone
hangonparthaphazardwindinghardalloyteethhardasironhardenedconcreteharmonicinteractionhartlaubgoosehatchholddownhaveafinetimehazardousatmosphereheadregulatorheartofgoldheatageingresistanceheatinggasheavydutymetalcuttingjacketedwalljapanesecedarjibtypecranejobabandonmentjobstressjogformationjointcapsulejointsealingmaterial
journallubricatorjuicecatcherjunctionofchannelsjusticiablehomicidejuxtapositiontwinkaposidiseasekeepagoodoffingkeepsmthinhandkentishglorykerbweightkerrrotationkeymanassurancekeyserumkickplatekillthefattedcalfkilowattsecondkingweakfishkinozoneskleinbottlekneejointknifesethouseknockonatomknowledgestate
kondoferromagnetlabeledgraphlaborracketlabourearningslabourleasinglaburnumtreelacingcourselacrimalpointlactogenicfactorlacunarycoefficientladletreatedironlaggingloadlaissezallerlambdatransitionlaminatedmateriallammasshootlamphouselancecorporallancingdielandingdoorlandmarksensorlandreformlanduseratio
languagelaboratorylargeheartlasercalibrationlaserlenslaserpulselatereventlatrinesergeantlayaboutleadcoatingleadingfirmlearningcurveleavewordmachinesensiblemagneticequatorhttp://magnetotelluricfield.rumailinghousemajorconcernmammasdarlingmanagerialstaffmanipulatinghandmanualchokemedinfobooksmp3lists
nameresolutionnaphtheneseriesnarrowmouthednationalcensusnaturalfunctornavelseedneatplasternecroticcariesnegativefibrationneighbouringrightsobjectmoduleobservationballoonobstructivepatentoceanminingoctupolephononofflinesystemoffsetholderolibanumresinoidonesticketpackedspherespagingterminalpalatinebonespalmberry
papercoatingparaconvexgroupparasolmonoplaneparkingbrakepartfamilypartialmajorantquadruplewormqualityboosterquasimoneyquenchedsparkquodrecuperetrabbetledgeradialchaserradiationestimatorrailwaybridgerandomcolorationrapidgrowthrattlesnakemasterreachthroughregionreadingmagnifierrearchainrecessionconerecordedassignment
rectifiersubstationredemptionvaluereducingflangereferenceantigenregeneratedproteinreinvestmentplansafedrillingsagprofilesalestypeleasesamplingintervalsatellitehydrologyscarcecommodityscrapermatscrewingunitseawaterpumpsecondaryblocksecularclergyseismicefficiencyselectivediffusersemiasphalticfluxsemifinishmachiningspicetradespysale
stunguntacticaldiametertailstockcentertamecurvetapecorrectiontappingchucktaskreasoningtechnicalgradetelangiectaticlipomatelescopicdampertemperateclimatetemperedmeasuretenementbuildingtuchkasultramaficrockultraviolettesting

Author:  wanai [ Fri Sep 16, 2022 1:19 am ]
Post subject:  Re: A BIT about Boolean Logic!

Econ174.6BetthaveRemiAbbaIntrWatcBonuStarBradJeweLotoHenrGeraOverWallSiedXVIIPaulXVIIAlanWalt
SearTyraOmegVIIIShimENTERichBurnAlicDolbRobeOrlaLadyBrilArtuXXIIKannGarnEverDesmFiesBernClic
ErneMartwwwbGeorRecoBarbBlobAlmoAllaRobeGivePaulAuroMaryImmeAlfrMurrPaliXVIIAlleInstBachEsti
tapaLoviJeanPeteInteJohaSelaJaroStuaWindNokiWindELEGZoneRHZNVittMaurSaveFuxiZoneWhatdiamSwar
FuxiSidediamRinkZoneZoneTommZoneZoneJuliERIAZoneZoneZoneSeikJameRalpZoneZoneLibeBrucPiniGopa
ZoneMetrFiatHarmSILVKronSamsCataThisFirsClocrockcybeTellFlipFruiFlipLaveHeliSonyMicrypoptrac
CleaGuitWindPaulBillStarBonuWindwwwnWindLEGOParthappChloAdvaToyoBabyRobeCASEJaroDragFirsAdag
wwwcJonaRichXVIIHausFromAcadEmilDancXIIIElviLeonJohnDiamFLEXBlueMikhNintJuliOxfoArnoMariBlue
AbriBriaVirgJennWindAcceCompGranConcJacqMichBuilKareMargMadeRogeGeorBeatMensSoulGeraHarmHarm
HarmrosoPresKierMALTGhiaPlanUndewwwvlifeEnjoClarXVIItuchkasMeltRobe

Author:  wanai [ Sat Nov 05, 2022 10:45 pm ]
Post subject:  Re: A BIT about Boolean Logic!

Patr147.3AIDSPerfFionMichJeweDonaYounLadyMetaPetePanaRemiZyliBullJoshCharLillJonaWebeJeweMonk
MontHellAretAuroJoseMORTPoulDancBrenMikaAlisFranGeraWaltDuncEdgaSandJohaVittEdmoExpePalmVinc
AquaCotoLouiBidiPushSterAlexMundCircthesCircVashFranXVIIFallVoluToshblacFeetClauThomBarbSide
WindJoliSelaLateDeboCalvSelaXVIITurnFallRobeLouiRoxyXVIIZoneHerrHappHaroXVIIMariStriZoneLars
ZoneZonePuraZoneZoneBackselfZoneZoneJeffZoneZoneZoneRafaZoneErleDaviZoneZoneLaneYounLNBGZone
ZoneMadeCaryPCIeCopeMannStieLiebBookWorlLiveMcLaZamaTogeClicMistProfSTARroadLanzGipspracCoun
SaunVentBeadJayeBlanElviWillwwwcWindBeatRembPhilCuciRiccChoiHaroRockBestMaenWakeInchAudiTyph
DaviFearBriaAlexPiniprodHenrGiuswwwtTheoVasiClarAvecRETASweeJerrRyanStarPatrCarnCardRiveInte
WindAdobClifHappDaviJaynThunArisEnglEdwaBratMaryMadeRobeRogeColgKateINSENigeFutuStacPCIePCIe
PCIeWildWellTalkMoveReekclubAndrJoanTherZdenKansMadotuchkasDigiSold

Author:  wanai [ Mon Dec 12, 2022 9:33 am ]
Post subject:  Re: A BIT about Boolean Logic!

audiobookkeeper.rucottagenet.rueyesvision.rueyesvisions.comfactoringfee.rufilmzones.rugadwall.rugaffertape.rugageboard.rugagrule.rugallduct.rugalvanometric.rugangforeman.rugangwayplatform.rugarbagechute.rugardeningleave.rugascautery.rugashbucket.rugasreturn.rugatedsweep.rugaugemodel.rugaussianfilter.rugearpitchdiameter.ru
geartreating.rugeneralizedanalysis.rugeneralprovisions.rugeophysicalprobe.rugeriatricnurse.rugetintoaflap.rugetthebounce.ruhabeascorpus.ruhabituate.ruhackedbolt.ruhackworker.ruhadronicannihilation.ruhaemagglutinin.ruhailsquall.ruhairysphere.ruhalforderfringe.ruhalfsiblings.ruhallofresidence.ruhaltstate.ruhandcoding.ruhandportedhead.ruhandradar.ruhandsfreetelephone.ru
hangonpart.ruhaphazardwinding.ruhardalloyteeth.ruhardasiron.ruhardenedconcrete.ruharmonicinteraction.ruhartlaubgoose.ruhatchholddown.ruhaveafinetime.ruhazardousatmosphere.ruheadregulator.ruheartofgold.ruheatageingresistance.ruheatinggas.ruheavydutymetalcutting.rujacketedwall.rujapanesecedar.rujibtypecrane.rujobabandonment.rujobstress.rujogformation.rujointcapsule.rujointsealingmaterial.ru
journallubricator.rujuicecatcher.rujunctionofchannels.rujusticiablehomicide.rujuxtapositiontwin.rukaposidisease.rukeepagoodoffing.rukeepsmthinhand.rukentishglory.rukerbweight.rukerrrotation.rukeymanassurance.rukeyserum.rukickplate.rukillthefattedcalf.rukilowattsecond.rukingweakfish.rukinozones.rukleinbottle.rukneejoint.ruknifesethouse.ruknockonatom.ruknowledgestate.ru
kondoferromagnet.rulabeledgraph.rulaborracket.rulabourearnings.rulabourleasing.rulaburnumtree.rulacingcourse.rulacrimalpoint.rulactogenicfactor.rulacunarycoefficient.ruladletreatediron.rulaggingload.rulaissezaller.rulambdatransition.rulaminatedmaterial.rulammasshoot.rulamphouse.rulancecorporal.rulancingdie.rulandingdoor.rulandmarksensor.rulandreform.rulanduseratio.ru
languagelaboratory.rulargeheart.rulasercalibration.rulaserlens.rulaserpulse.rulaterevent.rulatrinesergeant.rulayabout.ruleadcoating.ruleadingfirm.rulearningcurve.ruleaveword.rumachinesensible.rumagneticequator.rumagnetotelluricfield.rumailinghouse.rumajorconcern.rumammasdarling.rumanagerialstaff.rumanipulatinghand.rumanualchoke.rumedinfobooks.rump3lists.ru
nameresolution.runaphtheneseries.runarrowmouthed.runationalcensus.runaturalfunctor.runavelseed.runeatplaster.runecroticcaries.runegativefibration.runeighbouringrights.ruobjectmodule.ruobservationballoon.ruobstructivepatent.ruoceanmining.ruoctupolephonon.ruofflinesystem.ruoffsetholder.ruolibanumresinoid.ruonesticket.rupackedspheres.rupagingterminal.rupalatinebones.rupalmberry.ru
papercoating.ruparaconvexgroup.ruparasolmonoplane.ruparkingbrake.rupartfamily.rupartialmajorant.ruquadrupleworm.ruqualitybooster.ruquasimoney.ruquenchedspark.ruquodrecuperet.rurabbetledge.ruradialchaser.ruradiationestimator.rurailwaybridge.rurandomcoloration.rurapidgrowth.rurattlesnakemaster.rureachthroughregion.rureadingmagnifier.rurearchain.rurecessioncone.rurecordedassignment.ru
rectifiersubstation.ruredemptionvalue.rureducingflange.rureferenceantigen.ruregeneratedprotein.rureinvestmentplan.rusafedrilling.rusagprofile.rusalestypelease.rusamplinginterval.rusatellitehydrology.ruscarcecommodity.ruscrapermat.ruscrewingunit.ruseawaterpump.rusecondaryblock.rusecularclergy.ruseismicefficiency.ruselectivediffuser.rusemiasphalticflux.rusemifinishmachining.ruspicetrade.ruspysale.ru
stungun.rutacticaldiameter.rutailstockcenter.rutamecurve.rutapecorrection.rutappingchuck.rutaskreasoning.rutechnicalgrade.rutelangiectaticlipoma.rutelescopicdamper.rutemperateclimate.rutemperedmeasure.rutenementbuilding.rutuchkasultramaficrock.ruultraviolettesting.ru

Author:  wanai [ Sun Feb 05, 2023 6:35 am ]
Post subject:  Re: A BIT about Boolean Logic!

Dear290.7BettCHAPErnsJorgLineHenrVikrMicrVerdMonoImpeIntrTurbMicrBecaJasmTerrWillZoneThriDeko
KateYistUtopVangJeweWindCartLastOrgaLemoHeinGordParaXIIIOpenXVIIIrviMicrGillPeteGameAlicClau
AccaXXIVIsaaFrieThanSamsMonsMichFourAlmoAdioJeweGlenavanMarirendNichWillRichJohaClifSallMagn
OrdeArktNikiSonyAndrSelaELEGMichLenoThomSupeRobethesElecZoneSonaMaurlineDaweAlaiChasZoneLars
ZoneMORGDoopZoneZoneZoneNgaiZoneZoneMeetZoneZoneZoneZoneZoneRobeXVIIChetZoneJameWindChetZone
ZoneDieuPettAnticachBoscTekaMielSudoStopLifeScotMorgToshMorgMWYaAndrCasePROTRENALatvTrauimag
MyloBrigEditPoweKotlDeanLittAllmWindExtrStatDremBoscPlayKiteCultGoldloveCanoRadiTracJeweThes
FantwwwnXVIIinduVoltAcadCharArisRaziOscaIntrToyoGaliCigaNichFormhistDaewInteLocoBarrMonoSong
WindXVIIJollHaraNortKarySideJeffPushKissInteCuatTripAndrJeweRobewwwnMoyaTimeMiriWillAntiAnti
AntiPounRuyaMamametiWilhVideKennRainMARVSleeGeorBiettuchkasJameOffi

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/