Laran Evans
I develop software people love to use.
  • Home
  • About
  • Contact
  • Resume
  • Testimonials
Skip to content
  • Entrepreneurship
    • Leadership
    • Project Management
    • Team Development
    • Time Management
  • Motorcycles
    • Restoration Projects
  • Puzzles
  • Software Architecture
    • Cloud Computing
  • Software Development
    • Algorithms
    • CSS
    • Java
    • Javascript
    • MySQL
    • PHP
    • Python
    • Ruby and Rails
    • System Administration
« Kuali in Nacubo Magazine
Ruby Garbage Collection In-Depth »

Binary Logic Basics

By laran | Published: 2010/02/21

Binary operations are handy little tools for computer scientists.

A lot of developers who have picked up programming along the way, and not received a rigorous education in the fundamentals of computer science might not see the value in such low-level structures. But, they are very useful under certain circumstances. Bit-arrays (which can be represented just as integers) can pack a tremendous amount of information into a tiny amount of space. And this really counts for a lot when you’re talking about an application that needs to scale.

It’s not just size that’s on your side with bit-arrays, but also speed. Bitwise operations are the fastest logical operations a computer can perform. So if you need to execute something over and over, if you can do it with bitwise operators you’ll see some screaming performance.

Each of the examples below will test the nth bit (from right to left) in the integer x.

Set a Bit

Use OR.

x | (1 << n)

Test a Bit

Use AND.

x & (1 << n)

Clear a Bit

Use XOR.

x ^ (1 << n)

Toggle a Bit

Use NOT.

x & ~(1 << n)

This entry was posted in Algorithms and tagged Algorithms, bits, computer science, fundamentals, logic. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
« Kuali in Nacubo Magazine
Ruby Garbage Collection In-Depth »

2 Comments

  1. Sean
    Posted 2010/02/21 at 10:30 PM | Permalink

    Do these actually work in some language?

  2. laran
    Posted 2010/02/24 at 10:15 PM | Permalink

    Not that I know of. You should be able to translate the concept into the language of your choice though. The operators I used are pretty standard.

Post a Comment

Click here to cancel reply.

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • About Me

    I've got a masters degree in computer science and over 10 years of experience building web-based systems using Java/J2EE, Ruby, Rails and PHP. I'm a strong believer in the effectiveness of Agile Methods. Read more »

  • Subscribe

  • Table of Contents
    • Set a Bit
    • Test a Bit
    • Clear a Bit
    • Toggle a Bit
  • Similar Posts
    • All Files Recursively
    • Quicksort
    • AtoI
    • String Permutations
    • Arithmetic from Addition
Hello world
  • From Around the Web

      Shared Items
    • Recent Posts

      • The carbs are clean!
      • 80 pages of Ruby on Rails Performance Optimization Tips
      • Ruby Garbage Collection In-Depth
      • Binary Logic Basics
      • Kuali in Nacubo Magazine
    • Older Posts By Month

      Let's Talk

      Ask a question below. You'll be prompted for your name and email after you click the "Ask" button.

    Know more. Accomplish more. Succeed.