Pages: [1] 2   Go Down
Author Topic: bitwise problem  (Read 923 times)
0 Members and 1 Guest are viewing this topic.
Illinois
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am working on a nixie clock.  In 12 hour mode I want to blank the leading zero.

This code does not work:
Code:
if  (hours & 0b00010000 == 0)  // check to see if the hours = 10, 11, or 12
  {

    hoursOut = hours | leadzeroblank;      //if it is not 10, 11, or 12 blank the leading zero
  }
  else
  {
    hoursOut = hours;
    // Serial.println(hours,BIN);
  }

and this code does work:
Code:
  byte tens = hours & 0b00010000;    // mask the least sig byte of the 10's data
  if  (tens == 0)  // check to see if the hours = 10, 11, or 12
  {

    hoursOut = hours | leadzeroblank;      //if it is not 10, 11, or 12 blank the leading zero
  }
  else
  {
    hoursOut = hours;
  }

Anyone have any idea why the first set of code does not give me the desired result?
What I am trying to do is check for the MSB of my hours data being equal to 1 as in 10, 11, or 12 o'clock.
If it is not then I send a code to my nixie driver that it can not understand and it blanks the digit.

First code does not work and the second set does.  There is something wrong with the IF statement.
and I do not know what is wrong. 

Thanks in advance.

RWW
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this.  It might work.
Code:
if  ((hours & 0b00010000) == 0)  // check to see if the hours = 10, 11, or 12
  {

    hoursOut = hours | leadzeroblank;      //if it is not 10, 11, or 12 blank the leading zero
  }
  else
  {
    hoursOut = hours;
    // Serial.println(hours,BIN);
  }

I can't remember the precedence off the top of my head, but & and == are both operators like + or *.  You need to check precedence.
Logged

http://ilektron-x.blogspot.com - Hacking and General Electronics Goodness

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13479
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Anyone have any idea why the first set of code does not give me the desired result?

What is the datatype of hours?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the datatype of hours?
BCD I assume, from the bitwise and and the comments. And if so, it is indeed a precedence issue, as suggested above
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13479
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is used as BCD from the logic but the datatype can be byte, int, char unsigned etc.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33321
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Try this.  It might work.
Yes ilektron, spot on this will work.
It is a trap that is common I sometimes succumb to it myself.
Logged

Illinois
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I made the change in the code suggested by ilektron.  The code does indeed now work as I
envisioned.  Thank you!!!!

Now can someone please help me understand  what is meant by the term precedence used in this discussion.

 RWW
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
what is meant by the term precedence used in this discussion.
Precedence == order of evaluation.
Multiplication and division have higher precedence than addition and subtraction, so you can write:
int y = 7 * 4 + 9;
and know that the 7 * 4 will be performed, and then 9 will be added, as opposed to 4 and 9 being added, then multiplied by 4.

Your assignment now is to find the where modulo (%), logical and (&) and logical or (|) fit with respect to multiplication/division and addition/subtraction.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13479
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In addition to PaulS remark,

int y = 9 + 7 * 4;

will be evaluated the same way as multiplication will come first.

Enforcing evaluation can be done with () which may cause a bit more typing but at least you get what you want smiley

details see - http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Illinois
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the further info.  I think i understand precedence now.

As for my assignment...

multiplication       *
division                 /
addition                +
subtraction           -
Bitwise AND          &
Bitwise  OR            |

in order from top to bottom.  Correct?

Does MODULO refer to the remainder of a division calculation?

RWW
Logged

Melbourne, Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 219
Have you tried turning it off and on again?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does MODULO refer to the remainder of a division calculation?

yes.
Logged

Illinois
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Then modulo would fit between division and addition....  Correct?

RWW
Logged

Melbourne, Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 219
Have you tried turning it off and on again?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is from "The Complete Reference: C++", Table 2-8 "The Precedence of C Operators"

Highest:
()[]->
!~++--(type)*&sizeof
*/%
+-
<<>>
< <= > >=
== =!
&^
|
&&
||
Highest:
?:
= += -= *= /= etc.
Lowest:
,
Logged

Melbourne, Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 219
Have you tried turning it off and on again?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I usually just use () for readability; even if they are not needed.

i.e.
if( a+b <= MAGIC_NUMBER/2) ...

I would type...
if( (a + b) <= (MAGIC_NUMBER / 2) )...

Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 329
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I always use parentheses for things like this. I'd rather spend a couple of seconds making absolutely sure evaluations go the way I intend than hours trying to figure out what went wrong. Bitter experience speaking here.
Logged

Pages: [1] 2   Go Up
Jump to: