Set a bit, clear a bit.

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
ludoVIC
Posts: 31
Joined: Fri Jan 08, 2021 8:36 am

Set a bit, clear a bit.

Post by ludoVIC »

While reading multiple sources, I often find the terms "setting / clearing" a bit.
Now, I am not a native speaker, plus my experience with low-level programming is zero.
We also say "the devil is in the detail", therefore...I honestly prefer to ask :mrgreen:

I think that:
- set a bit = put its value to 1
- clear a bit = put its value to 0

May I ask if I understood correctly? Thanks in advance.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Set a bit, clear a bit.

Post by tokumaru »

That's correct.
ludoVIC
Posts: 31
Joined: Fri Jan 08, 2021 8:36 am

Re: Set a bit, clear a bit.

Post by ludoVIC »

Thank you
User avatar
za909
Posts: 249
Joined: Fri Jan 24, 2014 9:05 am
Location: Mijn hart woont al in Nederland

Re: Set a bit, clear a bit.

Post by za909 »

The original question has been answered, but as a tip, the following instructions are used for these operations:
ORA - set bits of A that are '1' in the operand
AND - clear bits of A that are '0' in the operand
EOR - flip bits of A that are '1' in the operand

For example if I had a variable that holds bit flags and I wanted to set only the highest two bits, I can do this:

Code: Select all

lda variable
ora #%11000000
sta variable
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Set a bit, clear a bit.

Post by Pokun »

Also "reset" is a synonym to "clear" when it comes to bit manipulation.
ludoVIC
Posts: 31
Joined: Fri Jan 08, 2021 8:36 am

Re: Set a bit, clear a bit.

Post by ludoVIC »

Thank you, that's helpful to know! :mrgreen:
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Set a bit, clear a bit.

Post by unregistered »

OR can be represented by + or |
1 + 1 = 1
1 + 0 = 1
0 + 1 = 1
0 + 0 = 0

AND can be represented by * or &
1 * 1 = 1
1 * 0 = 0
0 * 1 = 0
0 * 0 = 0

XOR (used in the EOR instruction on the NES)
1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0


Often I find starting at the foundation of an idea much simpler than trying to learn from a summary. :)


edit: the ORA instruction, on the NES, performs an OR between each pair of bits... i.e. bit0 in the accumulator and bit0 in the value supplied to the instruction; same with each other bit pair: bit1 OR through bit7 OR; the result of those 8 ORs is stored in the accumulator (the bit0 OR winds up in bit0... etc.).

Same concept for the NES’ AND and EOR instructions. :)
ludoVIC
Posts: 31
Joined: Fri Jan 08, 2021 8:36 am

Re: Set a bit, clear a bit.

Post by ludoVIC »

Hello. Well, my initial confusion was actually just a linguistic one, but always good to have some tricks more :wink:
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Set a bit, clear a bit.

Post by unregistered »

ludoVIC wrote: Thu Jan 28, 2021 8:09 am Hello. Well, my initial confusion was actually just a linguistic one, but always good to have some tricks more :wink:
Hi ludoVIC. I do understand the thread problem had already been solved, but za909’s odd post influenced me to reply... I’m sorry for making this thread long. At the same time I’m pleased you seem to appreciate my post. :)
ludoVIC
Posts: 31
Joined: Fri Jan 08, 2021 8:36 am

Re: Set a bit, clear a bit.

Post by ludoVIC »

Hi, no worries at all. You're welcome; I think it's good to share ideas capable of helping a potential future reader :)
Thanks for the feedback and see you around here!
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Set a bit, clear a bit.

Post by unregistered »

ludoVIC wrote: Fri Jan 29, 2021 8:09 am Hi, no worries at all. You're welcome; I think it's good to share ideas capable of helping a potential future reader :)
Thanks for the feedback and see you around here!
:D You’re welcome. It’s been a pleasure meeting you ludoVIC!
Post Reply