Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 142
Registered: ‎12-15-2008
My Device: Not Specified

Bitwise operations not having any effect

Why does the following have no effect?

int x = 0;
x = x & 1;

The result of x should be 1 but this and no other bitwise operations I use have any effect. What am I doing wrong?
Thanks for the help.
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Bitwise operations not having any effect

[ Edited ]

The correct answer is 0. You are misunderstanding bitwise operations

 

0 & 1 = 00
& 01
----
0 = 00

1 & 3 = 01
& 11
----
1 = 01

2 & 10 = 0010
& 1010
------
2 = 0010

In order to get an answer of 1, use the OR operator.
Message Edited by mreed on 01-20-2009 12:14 PM
Developer
Posts: 85
Registered: ‎12-05-2008
My Device: Blackberry 8900 Javelin

Re: Bitwise operations not having any effect

I'm guessing you want to OR it or XOR it? | or ^ (caret) respectively
Developer
Posts: 142
Registered: ‎12-15-2008
My Device: Not Specified

Re: Bitwise operations not having any effect

Oh my God! I can't believe I did that. I've been writing large systems and embedded systems for 15 years using bitwise operations everyday and
for some reason I just ... idk.... brain took a hike a guess.
I've been out of work for 3 months so the stress is getting to me I guess.
Thanks!