x


C# Bitwise Operations

Is it possible to use Bitwise operations to be used as flags instead of using boolean algebra for testing?

I cannot find a source (C# textbook) that even discusses it. What C# text would be excellent for using with Unity?

Thanks CSDG

more ▼

asked May 26 '11 at 09:14 PM

UniteMage gravatar image

UniteMage
134 24 30 32

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

The bitwise operators are &, |, ^ for AND, OR and XOR (the logic operators are && and ||). You can find a more complete reference in:

http://www.cprogramming.com/tutorial/bitwise_operators.html

more ▼

answered May 26 '11 at 09:22 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

To use bitwise operators in logic decisions you should compare the result to 0:

if ((flag1 & flag2)!=0){ ... }

May 26 '11 at 09:24 PM aldonaletto

The problem when using a classic Finite State Machine is that only one state can be called at a time. So testing for these simultaneous operations algebraically would be a mess. If I could simply "OR" the stated bits that would give me the desired effect without the boolean mess. Setting up a switch to cycle through the states for example ATTACK and FIRE while both keys are pressed simultaneously.

Thanks for the info CSDG

May 26 '11 at 09:36 PM UniteMage
(comments are locked)
10|3000 characters needed characters left

I have it working - I cleared out extra states that were redundant.

Thanks Again CSDG

more ▼

answered May 27 '11 at 01:57 AM

UniteMage gravatar image

UniteMage
134 24 30 32

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4376
x8

asked: May 26 '11 at 09:14 PM

Seen: 1595 times

Last Updated: May 27 '11 at 01:57 AM