Skip to main content

Followup to the Integer toggle code

My good friend Greg mentioned that I should use bitwise operators to perform a toggle, as opposed to my earlier method of using integer math to manipulate the state.  I had not done that because the bitwise implementation is imho not comprehensible by a developer who does not have a CS degree.

As an example it is not at ALL clear to most people that ~2<<10 != ~(2<<10)

Although I suppose that's the point of a method name.



/**
 * A simple way to implement a toggle switch using an integer to store the state
 * and bitwise operators to manipulate the toggle
 * 
 * @author Dan Fishman http://www.fishdan.com
 *
 * This code released to the public under the CreativeCommons 3.0 license by
 * the author on 8/21/2011
 * 
 * http://creativecommons.org/licenses/by/3.0/
 */

public class TestToggle {

  public static int toggleState=0;
  
  public static void main(String argv[]){
      for(int x=0;x<32;x++){
        System.out.println(x+":"+Integer.toBinaryString(toggleState));
        System.out.println(x+":"+Integer.toBinaryString(2 << x));
        toggle(true,x);
        System.out.println("-----------------------");
        System.out.println(x+":"+Integer.toBinaryString(toggleState));
        System.out.println("isOn "+x+" == "+toggleIsOn(x));
        toggle (false,x);
        System.out.println("isOn "+x+" == "+toggleIsOn(x));
        System.out.println("");
      }    
      
      //PAY ATTENTION TO THESE TWO LINES  order of operations can be tricky in bitwise
      // see http://www.uni-bonn.de/~manfear/javaoperators.php
      System.out.println("**"+Integer.toBinaryString(~2<<10));
      System.out.println("**"+Integer.toBinaryString(~(2<<10)));
  }

  /**
  * if onOff==true we set the value at pos == 1 aka on
  * else we set it to 0;
  * @param onOff
  * @param pos
  */
 public static void toggle(boolean onOff, int pos){
     if(onOff==true){
         toggleState |= 2 << pos;
     }
     else{
         toggleState &= ~(2<<pos);
     }
 }
 
 public static boolean toggleIsOn(int x){
     return (toggleState & 2<<x) == 2<<x;
 }        
    
}

Comments

Popular posts from this blog

Preventing accidental large deletes.

Instructions for Developers on Using the safe_delete Stored Procedure To enhance safety and auditability of delete operations within our databases, we have implemented a controlled deletion process using a stored procedure named safe_delete . This procedure relies on a temporary table ( temp_delete_table ) that lists complete records intended for deletion, not just their IDs. This approach helps prevent accidental deletions and provides a traceable audit log of delete actions. Why We Are Doing This Controlled Deletions : Centralizing delete operations through a stored procedure reduces the risk of erroneous or unauthorized deletions. Auditability : Using a temporary table to store complete records before deletion allows for an in-depth review and verification process, enhancing our ability to confirm and audit delete operations accurately. Security : Restricting direct delete permissions and channeling deletions through a specific proced...
 In software engineering, accumulating code behind a release wall is akin to gathering water behind a dam. Just as a dam must be built higher and stronger to contain an increasing volume of water, the more code we delay releasing, the more resources we must allocate to prevent a catastrophic flood—major bugs or system failures—while also managing the inevitable trickles—minor issues and defects. Frequent, smaller releases act like controlled spillways, effectively managing the flow of updates and reducing the risk of overwhelming both the system and the team. The ideal of ci/cd may not be achievable for all teams, but smaller and faster is always better.

October is Cyber Security Month

The President has declared October as Cybersecurity month.  It's not a bad idea -- just like you change the batteries in your smoke detectors once a year, maybe you should review your electronic vulbnerabilities? My top ten security tips: 1) Change your passwords.  You've had them too long, you use the same password in too many places.  Somewhere someone has hacked a site that has your username and password in plain text.  Now they are getting ready to try that username/password somewhere else.  Beat them to the punch. 2) Use a safe browser.  That means anything that's not Internet Explorer.   I prefer chrome. 3) Use 2 step verification for your email account.  If your email doesn't provide 2 step authentication consider switching. 4) Get a free credit report  and review it.  You are entitled to one free report a year.   BE VERY CAREFUL!  There are man scam sites that offer free credit reports.  Go through the s...