Cannot do programming 101....

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Freaks,

Been looking at a line of code from a freak from the past that I am having an issue with understanding.

uint8_t button_scan;
uint8_t buttons = 0;
uint8_t butos = 0;
uint8_t last_buttons = 0;

.
.

buttons = ((button_scan & 0x04) == 0);  //<----this is bugging me
butos = buttons && !last_buttons;
last_buttons = buttons;

.
.
.
.

button_scan is loaded with a value in an ISR...Thats all well and good, and when the code hits the line above is where I am getting hung up over what is exactly going on.  It seems that 'buttons' equals the bitwise AND of button_scan with 0x04 (0x04 isolates the bit to test), thats all and well, but I cannot understand what the '== 0' is all about. 

 

I am guessing that this:

((button_scan & 0x04) == 0)

is the same as:

if((button_scan & 0x04) == 0)

which means that if button_scan and 0x04 == 0,..... buttons = 1.

 

Correct?

 

Jim

This topic has a solution.

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Wed. Sep 12, 2018 - 12:47 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes

((button_scan & 0x04) == 0)

is a boolean expression which is either true or false

 

EDIT

 

This could have been made clearer by declaring buttons as a bool, and giving it a more meaningful name; eg, button_is_pressed.

 

EDIT 2

 

The "== 0" is actually superfluous

(button_scan & 0x04)

can also be taken as a boolean expression:

 

  • It is true when bit 2 is set in button_scan;
  • It is false when bit 2 is clear in button_scan.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Sep 11, 2018 - 08:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Andy, if you dont like the way its written you do not have to answer either.

 

Thank you for your reply though, I know its either true or false, but I was wondering if the way I am understanding it is correct with regards to the use of 'IF'.  Also since the and-ing being done, ie(button_scan & 0x04) would give a true or false, is it really necessary to have the '== 0' at the end?

 

 

JIm

 

EDIT:

I see you did an edit or two while I was typing! smiley

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Tue. Sep 11, 2018 - 08:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
Also since the and-ing being done, ie(button_scan & 0x04) would give a true or false, is it really necessary to have the '== 0' at the end?

 

Yes because you are actually inverting the logical value, so if value == 0, you get 1, and if value != 0 you get zero.

So it would be equivalent to:

 

buttons = !(button_scan & 0x04);
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

El Tangas wrote:

jgmdesign wrote:
Also since the and-ing being done, ie(button_scan & 0x04) would give a true or false, is it really necessary to have the '== 0' at the end?

 

Yes because you are actually inverting the logical value, so if value == 0, you get 1, and if value != 0 you get zero.

So it would be equivalent to:

 

buttons = !(button_scan & 0x04);

 

Yabbut, I always thought that '=' sets the value, and '==' is comparing to.

 

For example:

if(a == b)
{
    //do something if a is equal to b
}

and....

a = b;

//sets a to the same value as b

 

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
Yabbut, I always thought that '=' sets the value, and '==' is comparing to.

 

That's correct, but here you have one of each:

buttons = ((button_scan & 0x04) == 0);

So, if button_scan is 0x04, you get:

buttons = (0x04 == 0);

Since 0x04 is different from zero, the comparison takes value zero (false), so you get:

buttons = 0;

 

 

 

OTOH, if button_scan was zero, you would get:

buttons = (0 == 0);

The comparison would be true, so it would take value 1 (boolean true)

buttons = 1;

 

 

So, basically, buttons becomes 1 if bit 0x04 is clear, and 0 if it is set, that is, it takes the opposite value as the bit.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ahhh! I see now(no pun intended)  I also looked it up in My Smileymicros book and theres a simple one line explanation that is very similar to yours.  Thanks!

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

off topic

 

mentioning smileymicros... it appears that Joe has closed up shop. Had anyone noticed that before?

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

valusoft wrote:

off topic

 

mentioning smileymicros... it appears that Joe has closed up shop. Had anyone noticed that before?

Joe and I email on occasion and last time he said he was going to cut back a little.  I know he ws having website issues so if the sites not up I wouldnt worry too much.  I'll reach out to him and see whats up.

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user