Forum

Whats the difference?

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

Whats the difference?

Postby Ghost_Fang » Sat Sep 18, 2010 10:41 pm

I have used "&&" many times which means "and" right?
I have used "||" which is "or" correct?
I have recently came across " | " which looks to me like it means "and" as well.
So what is the difference between "&&" and "|"?
Ghost_Fang
 
Posts: 336
Joined: Thu Nov 12, 2009 4:37 am

Re: Whats the difference?

Postby GiffE » Sun Sep 19, 2010 12:50 am

Ghost_Fang wrote:I have used "&&" many times which means "and" right?
I have used "||" which is "or" correct?
I have recently came across " | " which looks to me like it means "and" as well.
So what is the difference between "&&" and "|"?


Incorrect, | and & are bitwise operators meaning & is "bitwise and" and | is "bitwise or". You may have to understand a bit of binary to fully understand them. In quakec they are most commonly used with flags as flags are powers of 2.

http://en.wikipedia.org/wiki/Bitwise_operation
GiffE
 
Posts: 170
Joined: Sun Oct 08, 2006 3:39 pm
Location: USA, CT

Postby r00k » Sun Sep 19, 2010 8:02 am

kinda like

if (self.var & (A|BlC)) to determine if var is any combination of ABC

u can also do

self.var = self.var - (self.var & blah);
or
self.var = (self.var|blah); which turns blah bit ON in .var

a bit of binary


Lol! no pun intended i'm sure ;)
r00k
 
Posts: 1110
Joined: Sat Nov 13, 2004 10:39 pm

Postby dreadlorde » Sun Sep 19, 2010 1:13 pm

Let's say that the variable `x` has a value of 13, and `y` a value of 32. In
binary, 13 is represented as `11001`. To understand this, I've made this table:
Code: Select all
128 64 32 16 8 4 2 1
0   0  0  0  1 1 0 1

As you can infer from this, `y` would be written as `100000`. With a bitwise or,
the result is 1 if the first bit is 1 or the second bit is 1, otherwise the result is 0.[1]
Code: Select all
 00001101
|00100000
---------
 00101101
Which equals 45.

With a bitwise and, the result is 1 if the first bit and the second bit are 1, and 0 otherwise.[1]
Code: Select all
 00001101
&00100000
---------
 00000000


Hope that helps.

[1]http://en.wikipedia.org/wiki/Bitwise_operation
Ken Thompson wrote:One of my most productive days was throwing away 1000 lines of code.

Get off my lawn!
User avatar
dreadlorde
 
Posts: 268
Joined: Tue Nov 24, 2009 2:20 am

Postby Ghost_Fang » Tue Sep 21, 2010 4:54 am

Yea, Baker explained a bit of binary before to me somewhere. And for once.... I completely understand! lol.

Thanks guys, I know a bit more now :D
Ghost_Fang
 
Posts: 336
Joined: Thu Nov 12, 2009 4:37 am


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest