C++ Boolean Algebra -


i have declared variable boolean , hoping c++ know when did boolean addition it's not happening way to. how work.

#include<iostream>  using namespace std;  int main() { bool x,j; x=0; j=1; for(int i=0;i<10;i++) { cout << x; x=x+j; } return 0; } 

i getting output as

011111111 

whereas hoping get

0101010101 

i hoping boolean variables mod out 2. if

x=1 x+1 = 0 x+1+1=1 x+1+1+1=0 

and on.

am confusing boolean algebra base-2 algebra?

thanks

bool x,j; x=x+j; 

this statement automatically promotes x , j type int before adding them. assignment converts bool in usual way: 0 becomes false, other number , including 2, becomes true.

you can z_2 addition using ^ (xor) operator instead:

x = x^j; 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -