Raised This Month: $ Target: $400
 0% 

variables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
FiFiX
Senior Member
Join Date: May 2008
Location: Poland
Old 08-15-2013 , 07:32   variables
Reply With Quote #1

Can someone explain to me how does variables like that works?

Definition:
Code:
#define VOTE_IN_PROGRESS    1
#define VOTE_FORCED            2
#define VOTE_IS_RUNOFF        4
#define VOTE_IS_OVER         8
#define VOTE_IS_EARLY        16
#define VOTE_HAS_EXPIRED    32
#define VOTE_OUTCOME        64
Usage:
check: g_voteStatus & VOTE_IS_RUNOFF
add: g_voteStatus |= VOTE_IS_RUNOFF
remove: g_voteStatus |= ~VOTE_IS_RUNOFF

f.e. If I'll add VOTE_OUTCOME to g_voteStatus it will be 64. It means that previous variables are already inside g_voteStatus?
g_voteStatus |= VOTE_IS_OUTCOME
and g_voteStatus & VOTE_IS_OVER will return true?
FiFiX is offline
Send a message via Skype™ to FiFiX
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 08-15-2013 , 07:34   Re: variables
Reply With Quote #2

Research bit values and bit comparisons
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
FiFiX
Senior Member
Join Date: May 2008
Location: Poland
Old 08-15-2013 , 08:49   Re: variables
Reply With Quote #3

It means if I'll use bitwise OR to add variable:
64 |= 32 will return 96
then while checking 96 & (16 || 8 || 4 || 2 || 1) will return 0, yes?
and 96 & (64 || 32) will not return 0.

If I'll use bitwise AND to add:
64 &= 32 it will return 32.

It also means that I can add one more variable
Code:
#define VOTE_NEW        128
And it won't affect any other, ye?
FiFiX is offline
Send a message via Skype™ to FiFiX
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 08-15-2013 , 16:11   Re: variables
Reply With Quote #4

You can add the last part without any problem.
64 &= 32 will return 0
If you want to add bits together you have to use bitwise operator |, not ||.
__________________

Last edited by Black Rose; 08-15-2013 at 16:17.
Black Rose is offline
FiFiX
Senior Member
Join Date: May 2008
Location: Poland
Old 08-15-2013 , 16:46   Re: variables
Reply With Quote #5

Quote:
Originally Posted by Black Rose View Post
64 &= 32 will return 0.
Why that will return 0?
FiFiX is offline
Send a message via Skype™ to FiFiX
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 08-15-2013 , 16:54   Re: variables
Reply With Quote #6

Quote:
Originally Posted by FiFiX View Post
Why that will return 0?
Because
Code:
  1000000
& 0100000
= 0000000
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 08-15-2013 , 17:03   Re: variables
Reply With Quote #7

Because:
1. I tested it, it returned 0.
2. Bitwise AND compares what the two parts have in common.
For example, We have 64(bit 7) and 32(bit 6) If we were to use bitwise AND on these. The result would be as follows:
Code:
	64	32
1	0	0	=0
2	0	0	=0
4	0	0	=0
8	0	0	=0

16	0	0	=0
32	0	1	=0
64	1	0	=0
128	0	0	=0
			=0
If on the other hand they would have something in common. Like 64(bit 7) and 96(bit 6 and 7) the result would be as follows:
Code:
	64	96
1	0	0	=0
2	0	0	=0
4	0	0	=0
8	0	0	=0

16	0	0	=0
32	0	1	=0
64	1	1	=64
128	0	0	=0
			=64
Compare this to the bitwise OR which basically adds up all the 1's into one result.
Code:
	64	96
1	0	0	=0
2	0	0	=0
4	0	0	=0
8	0	0	=0

16	0	0	=0
32	0	1	=32
64	1	1	=64
128	0	0	=0
			=96
This is why you add with |=. Both sides will combine all the activated(or whatever it's called? All the "1") bits into one larger array.
This is also why you check if a variable contains a certain bit with & because it will only return what both sides have in common.
__________________

Last edited by Black Rose; 08-15-2013 at 17:06.
Black Rose is offline
FiFiX
Senior Member
Join Date: May 2008
Location: Poland
Old 08-15-2013 , 17:22   Re: variables
Reply With Quote #8

i see. Thanks for explanation
FiFiX is offline
Send a message via Skype™ to FiFiX
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 15:50.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode