Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

adding 2 binary numbers

Status
Not open for further replies.

johnny78

Full Member level 4
Joined
Jun 28, 2017
Messages
206
Helped
0
Reputation
0
Reaction score
4
Trophy points
18
Activity points
1,698
hi Guys
one of the first lessons i've learned when started to learn programming was Numbers
but i forgot how to do this.

00010000
or
00000000
result
00010000

is it right i should use Or function ?
i've tried this with serial monitor to test what im doing but the result isnt what i need

Code:
int LedsDisplay = B00000000;
int PowerDisplay = B00010000;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Start");
  Serial.println(LedsDisplay);
  delay(1000);
  Serial.println(PowerDisplay);
  delay(1000);
  LedsDisplay =  ((LedsDisplay) || (PowerDisplay));
  Serial.println(LedsDisplay),BIN);
  delay(1000);
  int LedsDisplay = B00000000;
  int PowerDisplay = B00010000;
  delay(1000);
}
help me with this please

sorry for my english maybe i couldnt explain what i need exactly or do a useful search

thanks
Johnny
 
Last edited:

Hi,

is it right i should use Or function ?
you already used OR function....

but the result isnt what i need
I don´t know "what you need".
So, as always, give clear informations:
* what do you expect it to do?
* what does it do instead?
(maybe with examples, drawings...)

****

OR and ADD are different functions.
use OR when you want to OR
but use ADD when you want to ADD.

example: binary:
001 OR 011 = 011
001 ADD 011 = 100
001 AND 011 = 001

Klaus
 

Hi,


you already used OR function....


I don´t know "what you need".
So, as always, give clear informations:
* what do you expect it to do?
* what does it do instead?
(maybe with examples, drawings...)

****

OR and ADD are different functions.
use OR when you want to OR
but use ADD when you want to ADD.

example: binary:
001 OR 011 = 011
001 ADD 011 = 100
001 AND 011 = 001

Klaus
Hi

so OR is the right function i need but something wrong is going on
my code is giving another results

the result on serial monitor
_____________________
Start
0
16
1
Start
1
16
1
_____________________

i should get

Start
0
16
16

as example of what i need

a = 00001100
b = 00100000
_______________ Result
c =00101100

a = 00010000
b = 00100000
_______________ Result
c =00110000

thanks
Johnny
--- Updated ---

thanks Mr
i've found what i need exactly
my fault was using || instead of |

regards
Johnny
Screenshot (3).png
 
Last edited:

Hi,

to your code:
What compiler and what IDE do you use?
If you get any compiler errors and warnings, please post them.

What worries me:
* "B00000000": it surely depends on compiler, but I know "&b00000000", "0b00000000" ... but no one with a "B" as first letter

LedsDisplay = ((LedsDisplay) || (PowerDisplay));
It looks like a "logical OR" instead of a bitwise OR.
so shouldn´t it be:
* (LedsDisplay | PowerDisplay);
* or (LedsDisplay OR PowerDisplay);

Serial.println(LedsDisplay),BIN);
This I expect to cause a compiler error.
It has one opening bracket, but two closing brackets.

****
BTW: I recommend to declare your variables explicitely how you want it to be.
instead of "int LedsDisplay ..."
better write "uint8_t LedsDisplay..." ( unsigned 8 bit )
--> so it´s clear that you force it to be an 8 bit variable without a SIGN bit.

"int" can depend on complier and processor and may be an 8 bit, 16 bit, 32 bit ... variable.

Also it´s a good idea to write in the first lines of code for which type of microcontroler the code is written. Some compiler need this, but you are free to write it as comment.
And especially if you post this code in public (forum) ...

Klaus
 
Last edited:

    johnny78

    Points: 2
    Helpful Answer Positive Rating
Hi,

to your code:
What compiler and what IDE do you use?
If you get any compiler errors and warnings, please post them.

What worries me:
* "B00000000": it surely depends on compiler, but I know "&b00000000", "0b00000000" ... but no one with a "B" as first letter


It looks like a "logical OR" instead of a bitwise OR.
so shouldn´t it be:
* (LedsDisplay | PowerDisplay);
* or (LedsDisplay OR PowerDisplay);


This I expect to cause a compiler error.
It has one opening bracket, but two closing brackets.

****
BTW: I recommend to declare your variables explicitely how you want it to be.
instead of "int LedsDisplay ..."
better write "uint8_t LedsDisplay..." ( unsigned 8 bit )
--> so it´s clear that you force it to be an 8 bit variable without a SIGN bit.

"int" can depend on complier and processor and may be an 8 bit, 16 bit, 32 bit ... variable.

Also it´s a good idea to write in the first lines of code for which type of microcontroler the code is written. Some compiler need this, but you are free to write it as comment.
And especially if you post this code in public (forum) ...

Klaus
hi
im using Arduino IDE & no i didn't got errors
maybe the compiler corrected it for me

anyway thanks for the valuable informations you've provided was not in mind at all

best regards
Johnny
 

im using Arduino IDE & no i didn't got errors
maybe the compiler corrected it for me
Interpreting a logic or as bitwise or is not correction, it's a fault.
The stanadard is pretty clear about this
The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int
 

    johnny78

    Points: 2
    Helpful Answer Positive Rating
Interpreting a logic or as bitwise or is not correction, it's a fault.
The stanadard is pretty clear about this
The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int
hi
i was talking about this error which Klaus mentioned about it
--------------------------------
Serial.println(LedsDisplay),BIN);
This I expect to cause a compiler error.
It has one opening bracket, but two closing brackets.
-----------------------------------------------------------
& i've used || operator by mistake instead of |

best regards
Johnny
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top