Saturday, August 20, 2011

Using an integer to implement a binary toggle switch in Java

We were talking about a way to use an integer to indicate teh state of a series of switches.  e.g. if there were 5 switches and the first 3 were on we would say think of that as 11100, or 28

Not a bad method -- you can very quickly toggle switches on and off by calling

toggle(boolean on,int position)

and you can quickly check it the toggle at a position is on by calling

toggleIsOn(int position);

The code snippet is below, comments welcome.  you have to declare toggleState yourself of course...;



public int toggleState=0;

       /**
     * if onOff==true we set the value at pos == 1 aka on
     * else we set it to 0;
     * @param onOff
     * @param pos
     */
    public void toggle(boolean onOff, int pos){
        if(toggleState==null){
            toggleState=0;
        }
        if(onOff==true){
            if(toggleIsOn(pos)){
                //do nothing, already on
            }
            else{
                //we want it on, and it is currently off
                toggleState+=2^pos;
            }
        }
        else{
            if(toggleIsOn(pos)){
                //It's on, and we want it off
                toggleState-=2^pos;
            }
            else{
                //it's off and we want it off -- do nothing
            }            
        }
    }
    
    public boolean toggleIsOn(int pos){
        if(toggleState==null || toggleState < (2^pos)) return false;
        //divide toggle state
        int multiple=toggleState/(2^pos);
                //if the switch is on, multiple will be an odd number, otherwise it will be even.
        return multiple%2==1;
    }

2 comments :

Dan said...

This was just pseudocode --

2 ^ pos is not how to do wexpojnents in java. Instead you should do Math.pow(2,pos);

kopiluwak nya said...

https://www.facebook.com/bantalsilikongrosir
http://bantalsilikon01.blogspot.com/2014/12/a.html
http://jualsangkarpleci.blogspot.com/2014/12/a.html
http://kopiluwakliar01.blogspot.com/2014/12/a.html
http://vvty.in/uncategorized/marinir-seo/

http://marinirseo.blogspot.com/2014/12/a.html
http://marinir-seo.blogspot.com/2014/12/a.html
http://bumbu-pecel-malang.blogspot.com/2014/12/a.html
http://distro-seo.blogspot.com/2014/12/a.html
http://restoran-seo.blogspot.com/2014/12/a.html
http://bantalmalangmurah.blogspot.com/2014/12/a.html
http://distrobantal.blogspot.com/2014/12/a.html
http://kesethandukmalang.blogspot.com/2014/12/a.html

http://tasya.marinirseo.web.id/?p=4http://caca.marinirseo.web.id/?p=4http://anne.marinirseo.web.id/?p=4http://jeannet.marinirseo.web.id/?p=4http://jelita.marinirseo.web.id/?p=4http://brenda.marinirseo.web.id/?p=1http://ruth.marinirseo.web.id/?p=4http://tasya1.marinirseo.web.id/?p=4http://caca1.marinirseo.web.id/?p=4http://anne1.marinirseo.web.id/?p=4http://jeannet1.marinirseo.web.id/?p=4http://jelita1.marinirseo.web.id/?p=4http://brenda1.marinirseo.web.id/?p=1http://ruth1.marinirseo.web.id/?p=4http://tasya2.marinirseo.web.id/?p=4http://caca2.marinirseo.web.id/?p=5http://anne2.marinirseo.web.id/?p=4http://jeannet2.marinirseo.web.id/?p=4http://jelita2.marinirseo.web.id/?p=4http://brenda2.marinirseo.web.id/?p=1http://ruth2.marinirseo.web.id/?p=4http://ruth3.marinirseo.web.id/?p=3http://jelita3.marinirseo.web.id/?p=3http://brenda3.marinirseo.web.id/?p=3http://jeannet3.marinirseo.web.id/?p=3http://anne3.marinirseo.web.id/?p=3http://caca3.marinirseo.web.id/?p=3http://tasya3.marinirseo.web.id/?p=3http://tasya4.marinirseo.web.id/?p=4http://caca4.marinirseo.web.id/?p=4http://anne4.marinirseo.web.id/?p=4http://jeannet4.marinirseo.web.id/?p=4http://jelita4.marinirseo.web.id/?p=4http://brenda4.marinirseo.web.id/?p=6http://ruth4.marinirseo.web.id/?p=4http://tasya5.marinirseo.web.id/?p=4http://caca5.marinirseo.web.id/?p=4http://anne5.marinirseo.web.id/?p=4http://jelita5.marinirseo.web.id/?p=4http://ruth5.marinirseo.web.id/?p=4
http://tasya.marinirseo.web.id/?p=4http://caca.marinirseo.web.id/?p=4http://anne.marinirseo.web.id/?p=4http://jeannet.marinirseo.web.id/?p=4http://jelita.marinirseo.web.id/?p=4http://brenda.marinirseo.web.id/?p=1http://ruth.marinirseo.web.id/?p=4http://tasya1.marinirseo.web.id/?p=4http://caca1.marinirseo.web.id/?p=4http://anne1.marinirseo.web.id/?p=4http://jeannet1.marinirseo.web.id/?p=4http://jelita1.marinirseo.web.id/?p=4http://brenda1.marinirseo.web.id/?p=1http://ruth1.marinirseo.web.id/?p=4http://tasya2.marinirseo.web.id/?p=4http://caca2.marinirseo.web.id/?p=5http://anne2.marinirseo.web.id/?p=4http://jeannet2.marinirseo.web.id/?p=4http://jelita2.marinirseo.web.id/?p=4http://brenda2.marinirseo.web.id/?p=1http://ruth2.marinirseo.web.id/?p=4http://ruth3.marinirseo.web.id/?p=3http://jelita3.marinirseo.web.id/?p=3http://brenda3.marinirseo.web.id/?p=3http://jeannet3.marinirseo.web.id/?p=3http://anne3.marinirseo.web.id/?p=3http://caca3.marinirseo.web.id/?p=3http://tasya3.marinirseo.web.id/?p=3http://tasya4.marinirseo.web.id/?p=4http://caca4.marinirseo.web.id/?p=4http://anne4.marinirseo.web.id/?p=4http://jeannet4.marinirseo.web.id/?p=4http://jelita4.marinirseo.web.id/?p=4http://brenda4.marinirseo.web.id/?p=6http://ruth4.marinirseo.web.id/?p=4http://tasya5.marinirseo.web.id/?p=4http://caca5.marinirseo.web.id/?p=4http://anne5.marinirseo.web.id/?p=4http://jelita5.marinirseo.web.id/?p=4http://ruth5.marinirseo.web.id/?p=4