Home       Printables       All Games       Math Games       Word Games       Daily       Reference       Strategy & More       Junior!       Problems

Beginner Programming Tips

Home Reference Programming Tips and Tricks

Each page contains a helpful programming tip and exercises which
encourage beginners to use what they've learned in a different situation.

Index Previous    Next    Teachers    About   

Bookmark and Share    Link To This Page

Bit Toggle: Switching From Zero To One and Back

Toggling a value between 0 and 1 is something that computer programmers do alot. It's useful if you are running a loop, and each time you pass through the loop you want to do an alternating action. For example, if you are creating a two-player game, the first time through the loop it's Player One's turn, but then the second time it's Player Two's turn, and so on.

Have you ever used the piece of code shown below?

Dim MyToggle As Integer

'Toggle The Value Between Zero And One
If MyToggle = 0 Then
   MyToggle = 1
Else
   MyToggle = 0
End If

The code above works. It's what I used to do when I first started programming. It's readable, and it's obvious what's happening. However, it isn't the most efficient way of toggling a value. There is a method which is slightly faster. Of course, slightly faster is irrelevant to most of us, but if you are programming a high speed game, you need to make every nanosecond count. Here's the alternate way of toggling a bit:

Dim MyToggle As Integer

'Toggle The Value Between Zero And One
MyToggle = 1 - MyToggle

That's it? Yes, that's it. Just one line of code. Think about it. If MyToggle = 1, when you subtract it from one, you get zero. And if it equals zero, when you subtract it from one you get one.

It's simple, and it's straightforward, and once you get used to seeing it, it's obvious what it's doing. It makes your code a little shorter and neater.

You could also use this code to toggle between one and two, but you need to make sure you initialize the toggle value to either one or two before you start the toggle process:

Dim MyToggle As Integer
MyToggle = 1

'Toggle The Value Between One And Two
MyToggle = 3 - MyToggle

Other Scenarios

  1. How would you toggle between zero and negative one, using just one line of code?
  2. How would you toggle between two and three?
  3. How would you toggle between one and three?
  4. How would you toggle between five and ten?

Index Previous    Next    Teachers    About   

Bookmark and Share    Link To This Page
"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.

Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using this site in a classroom situation, please visit the Teachers page.

More programming information and other tips can be found at Virtu Software's Ask Doug site.

 

 
Search For More Resources

Search For More Educational Resources

 



Member Features
Login
 

Printable
Hangman
Picture Word
Four-Scramble
Word Search
Blackberry Game
Telephone Game
Strange Hangman
Word Grid
Secret Word
Scrambled Word
One of These
 

Entrapment
Adders!
Zap!
Side By Side
One To Ten
Sub Triangles
Magical Squares
Math Scramble
Secret Number
Secret Number 2
Fractional Hi Lo
Concentration
Monty Hall Game
 

Trio Match
Treasure Hunt
Pirate's Map
Fizziks Tilt
Zero Gravity
Easter Egg Hunt
Quad Puzzle
Tic Tac Toe
Rotating Block
 

Math Tutoring
The Solar System
Codes
Slick Math!
The Golden Ratio
Programming
Search It Out!
 
Brainfood
Math HS
Maine Page
Calculus Page
 
Contact
About
Related Sites
Privacy
Link to TPS
 

Bookmarking and Linking
Bookmark/Link


Home       Printables       All Games       Math Games       Word Games       Daily       Reference       Strategy & More       Junior!       Problems