Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:04 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : AND, NOT, OR, etc.

Author Message
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 07:38am 09 Dec 2022
Copy link to clipboard 
Print this post

Having a senior moment.

Regardless of the current bit state


a=a or 1


Will set the bit but what's the short version of:


if a and 1 then a=a xor 1


Pretty sure that in other dialects it's:


a=a and not 1


Edited 2022-12-09 17:39 by Tinine
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5089
Posted: 08:11am 09 Dec 2022
Copy link to clipboard 
Print this post

I understand you are manipulating one bit in integer a.

Your second line (the requested functionality) will check the bit.
If it is zero, nothing happens
When the bit is one it will XOR the variable with that bit (in essence putting it to zero).

The outcome is always this particular bit set to zero.

So the AND with the negated bit is the correct answer.

When a is a boolean, then the shortest solution is:
a=0

PicomiteVGA PETSCII ROBOTS
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 08:22am 09 Dec 2022
Copy link to clipboard 
Print this post

Example: I am expecting a=254


a=255
a=a and not 1 'just want to reset b0
print a


The result is zero
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 08:32am 09 Dec 2022
Copy link to clipboard 
Print this post

a=a and INV 1
 
IanRogers

Senior Member

Joined: 09/12/2022
Location: United Kingdom
Posts: 151
Posted: 08:34am 09 Dec 2022
Copy link to clipboard 
Print this post

If you and with 254 you will preserve the rest of 'a' but reset b0

a = a and 254
I'd give my left arm to be ambidextrous
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 08:43am 09 Dec 2022
Copy link to clipboard 
Print this post




That's QB64


@matherp: That works! Cheers
Not in my DOS version though but it's not the latest.


Craig
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 08:47am 09 Dec 2022
Copy link to clipboard 
Print this post

I implemented INV in the CMM2 and have added it to most of my ports. It is the exact equivalent of the C ~ operator which is the standard way of clearing bits

To set bit n assuming numbering from 0

a |= (1<<n)

to clear bit n

a &= ~(1<<n)
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 08:58am 09 Dec 2022
Copy link to clipboard 
Print this post

  IanRogers said  If you and with 254 you will preserve the rest of 'a' but reset b0

a = a and 254



Sure, but what I like to do is:



const actuator_1 = &h01
const actuator_2 = &h02

outputs = outputs or actuator_1
outputs = outputs and inv actuator_2



Then I update the DOUTs using the PORT command.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025