PDA

View Full Version : Bit-wise And/Or



Praethus
12-17-2007, 03:59 PM
What is the symbol for bit-wise and/or?

Also, the proper use is:


(some binary number) (symbol) (some other binary number);

It will then return a binary number with 1's wherever the operation is true and zero's where it's false correct?

Example: 1010 & 1001 would yield 1000, correct?

pkmnfrk
12-17-2007, 07:13 PM
The symbols are & for and, and | for or. (also, I just used and three times in the same sentence without any sense of irony)

You can use them on any number at all, not just ones expressed as binary.

Example:


int foo;
foo = 1010b | 101b; //foo now equals 1111b
foo = 1010b & 1001b; //foo now equals 1000b

But, you have to be careful. They seem to have a very low priority, which could result in problems:


if(1010b & 1000b == 0) { ... } //Gives an "invalid cast error"

You should wrap them with parenthesis:


if((1010b & 1000b) == 0) { ... } a-ok!

Praethus
12-17-2007, 08:17 PM
Awesome, thanks!

Gleeok
12-17-2007, 08:30 PM
Interesting. So what exactally is the difference between & and &&, or | or ||? Also does foo have a special function?

..Ah might as well ask what 'do' does while i'm at it.

thanks.

pkmnfrk
12-17-2007, 09:22 PM
Interesting. So what exactally is the difference between & and &&, or | or ||?

& and | are bitwise operators, && and || are logical operators. Consider this:

"1 && 2"

1 is non-zero, and thus true. 2 is non-zero, and thus true. True AND True = True.

But, 1 is actually 1b, and 2 is actually 10b. 1b & 10b = 0b, which is actually false.

See wikipedia (http://en.wikipedia.org/wiki/Bitwise_operations) for more info.


Also does foo have a special function?

foo is one of a series of "dummy" variable names that people use to name variables whose names are irrelevant. foo, bar, baz, etc. There are more, but are generally not needed.


..Ah might as well ask what 'do' does while i'm at it.

do is used in a while loop. while has two forms:

while(expression) { code }

and

do {code} while(expression);

The two forms both execute 'code' repeatedly until 'expression' is false. The difference is that in the first one, the expression is evaluated before the code, whereas in the second one, the code is evaluated first.

This might be important if, for example, expression had side effects or if you want the code block to run at least once.


thanks.

No problem

Joe123
12-18-2007, 02:04 PM
That Wikipedia page scares me :(

Is it important that I learn that?
What are the uses of bitwise operators that I can't achieve with logical operators?

Praethus
12-18-2007, 05:29 PM
I needed to know, so I could tell solidity of a combo. The ComboS method returns a bitmask. I'll explain more in the tutorial I'm working on.

Joe123
12-18-2007, 06:11 PM
is this a tutorial for the whole of ZScript?

pkmnfrk
12-18-2007, 06:57 PM
That Wikipedia page scares me :(

Is it important that I learn that?
What are the uses of bitwise operators that I can't achieve with logical operators?


I needed to know, so I could tell solidity of a combo. The ComboS method returns a bitmask. I'll explain more in the tutorial I'm working on.

There we go. If you want to check to see if a particular bit is set, you need to use bitwise and. For example:


//pretend var is a variable containing a bitfield (such as the one returned from Screen->ComboS[])

if((var & 1b) != 0) { } //do something if the top-left corner of the tile is impassible

if((var & 1000b) != 0 { } //now we're checking the bottom-right.

I made functions for these in my Miniboss portal script. Reposted here for posterity:


int setbit(int v, int num, bool b) {

int mask = Pow(2, num);

if(b) {
return (v | mask);
} else {
if(getbit(v,num))
return(v - mask);
else
return(v);
}
}

bool getbit(int v, int num) {

if(num <= 0) return(false);

int ret = v >> num;
return((ret & 1b) == 1);
}

Note that setbit returns the new value, so you would use it like this:


var = setbit(var, 2, true);

Praethus
12-19-2007, 03:01 AM
Yes, I am working on a complete tutorial for ZScript. With 7 years experience as a computer science major, I think I can make one easy enough for everyone to understand.