Toggle a Bit with SQL
I am working on a management dashboard for a client, which uses some AJAX here and there. One simple but really nice feature is that you can enabled or disable things just by clicking on an icon, the state is updated in the background using AJAX (I also add some animation to let the user know that it has been updated).
I wanted to be able to toggle the state of the
bit field without a care of the current state. Here's how you can do that in SQL:
UPDATE companies SET active_ind = active_ind ^ 1 WHERE id = ?
I'm using a
^ Bitwise (Exclusive OR or XOR) operator here to do this. To understand what's going on here let's look at a truth table for XOR:
|p||q||p ^ q|
- Dan Dan
DECLARE @Trend AS BIT
SET @Trend = 0
SELECT @Trend, ABS(@Trend-1)
since when it turned into 0-1
UPDATE user SET vis=IF(vis&4,vis&~4,vis|4) WHERE id=$authuserid LIMIT 1
The value "4" can be replaced with 2^bitposition, ie. 1,2,4,8,16,32,64...
- Disable Flash Remoting on ColdFusion Servers
- HackMyCF Adds SSL/TLS Scanner
- IncompatibleClassChangeError after ColdFusion 11 Update 5
- Scope Injection in CFML
- Upload Files Directly to Amazon S3 using ColdFusion
- Minor JavaDocs.org Update
- nginx Directive rewrite is not terminated
- Using Mozilla's Certificate Authority List for Java SSL