Raised This Month: $ Target: $400
 0% 

[SOLVED] Bitwise: more compact code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
isotonic
AlliedModders Donor
Join Date: Jun 2011
Location: Moscow, Russia
Old 10-18-2012 , 16:09   [SOLVED] Bitwise: more compact code
Reply With Quote #1

Need help for bitwise operations.

PHP Code:
#define VIP_A (1<<0)
#define VIP_Z (1<<6)
 
new giVipFlags[32]
 
public 
XXX(id) {
  
giVipFlags[id] |= VIP_A  // set VIP_A flag
  
giVipFlags[id] &= ~VIP_Z // clear VIP_Z flag

Can public XXX(id) do the same using 1 line of code not 2?
__________________

Last edited by isotonic; 12-15-2013 at 11:49.
isotonic is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 10-18-2012 , 16:51   Re: Bitwise: more compact code
Reply With Quote #2

if you're sure that VIP_A is always unset and VIP_Z is always set in that function then you could use this i suppose:

giVipFlags[id] ^= (VIP_A|VIP_Z)
jimaway is offline
isotonic
AlliedModders Donor
Join Date: Jun 2011
Location: Moscow, Russia
Old 10-18-2012 , 17:35   Re: Bitwise: more compact code
Reply With Quote #3

Could you explain step-by-step that code?
__________________
isotonic is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-18-2012 , 20:22   Re: Bitwise: more compact code
Reply With Quote #4

Unfortunately, to my knowledge, you cannot add a bit and remove a bit in a single operation. OR will always set the bit(s) that exist in either operand and AND/NOT will remove the bit(s) that exists in the comparing operand.

Don't be concerned with the number of lines in your code, be concerned about the amount of actual code, which in this case can be reduced using macros.
__________________
Bugsy is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 10-18-2012 , 20:55   Re: Bitwise: more compact code
Reply With Quote #5

Code:
iFlags = ( iFlags & ~(1) ) | (2)
iFlags = ( iFlags | (2) ) & ~(1)
Both should do it, but see Bugsy's notes above.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 10-19-2012 , 19:22   Re: Bitwise: more compact code
Reply With Quote #6

Quote:
Originally Posted by isotonic View Post
Need help for bitwise operations.

PHP Code:
#define VIP_A (1<<0)
#define VIP_Z (1<<6)
 
new giVipFlags[32]
 
public 
XXX(id) {
  
giVipFlags[id] |= VIP_A  // set VIP_A flag
  
giVipFlags[id] &= ~VIP_Z // clear VIP_Z flag

Can public XXX(id) do the same using 1 line of code not 2?
SURE!
Code:
giVipFlags[id] |= VIP_A; giVipFlags[id] &= ~VIP_Z
__________________
What an elegant solution to a problem that doesn't need solving....
Liverwiz is offline
isotonic
AlliedModders Donor
Join Date: Jun 2011
Location: Moscow, Russia
Old 10-19-2012 , 19:40   Re: Bitwise: more compact code
Reply With Quote #7

Quote:
Originally Posted by Emp` View Post
Code:
iFlags = ( iFlags & ~(1) ) | (2)
iFlags = ( iFlags | (2) ) & ~(1)
Emp` did I understand you properly?
PHP Code:
giVipFlags[id] = ( giVipFlags[id] | VIP_A ) &~ VIP_Z 
--

Nice joke Liverwiz ;)
__________________
isotonic is offline
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 07:02.


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