intelliproject logo

Location: Computer science - Algorithms    License: The Intelliproject Open License (IPOL)

An introduction to bitwise operators

Posted by Silviu Caragea

This article gives a brief overview of C style bitwise operators

Skill: Beginner

Posted: 04/10/2008

Views: 424

Rating: 5.00 /5

Popularity: 0.00

Sign Up to vote for this article

Introduction

Because a lot of people seem to have problems with bitwise operators I had decide to write this short tutorial on how to use them.

Before I start explaining the bitwise operators I want to make a brief presentation of the notions used in this article.

  1. A bit is a binary digit, taking a value of either 0 or 1. Binary digits are a basic unit of information storage and communication in digital computing and digital information theory.
  2. A byte  is a collection of bits, originally differing in size depending on the context but now almost always eight bits. A byte (also known as octet), can represent 256 values (28 values, 0–255). A four-bit quantity is known as a nibble, and can represent 16 values (24 values, 0–15). A rarely used term, crumb, can refer to a two-bit quantity, and can represent 4 values (2² values, 0–3).
  3. WORD is a term for a slightly larger group of bits, but it has no standard size. It represents the size of one register in a Computer-CPU. In the IA-32 architecture more commonly known as x86-32, 16 bits are called a "word" (with 32 bits being a double word or DWORD), but other architectures have word sizes of 8, 32, 64, 80 or others.

data_representaion.jpg

Because is very hard (for us not for the computer) to work with binary notation (1 and 0) you will always use hexadecimal (base 16) numbering system.

hexadecimal.jpg

Bitwise operators

There are six bitwise operators:

  • &    AND
  • |    OR
  • ^   XOR
  • ~   NOT
  • >>  Right Shift
  • <<  Left Shift

operators.JPG

1. The AND (&) operator

A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0 (as you can see in the above table).


The bitwise AND may be used to perform a bit mask operation. This operation may be used to isolate part of a string of bits, or to determine whether a particular bit is 1 or 0.

For example say we have a BYTE that contains some bit flags, and we want to check if bit three is set (bit position start from 0).

2. The OR (|) operator
 
A bitwise OR takes two bit patterns of equal length, and produces another one of the same length by matching up corresponding bits (the first of each; the second of each; and so on) and performing the logical inclusive OR operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 OR the second bit is 1 (or both), and otherwise the result is 0 (see the above table).

An ideal use for this is to ensure that certain bits are set. Suppose we want to ensure that bit two of any value is set (without change any other bits).

3.  The XOR (^) operator

A bitwise exclusive or takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same (see the above table).

XOR has two useful properties:

  1. A XOR A = 0
  2. (A XOR B) XOR B = A

Far, most uses of the XOR are based on the second proprietary:

  • encryption methods (where A is the message and B is the key);
  • simple design methods that allow for adding and deleting items without redrawing the entire image (A – the image, B the new element).

Using XOR function we can reverse the value of a particular bit, the rest remaining unchanged. For example:

4. The NOT (~) operator

The bitwise NOT, or complement, is a unary operation which performs logical negation on each bit, forming the ones' complement of the given binary value. Digits which were 0 become 1, and vice versa (see the above table).

For example suppose we want to set all bits with 1 excepting bit 0 .

5 & 6. The Right Shift (>>) & Left Shift (<<) operators

The >> (Right shift) and << (Left shift) operators move the bits the number of bit positions specified. The >> operator shifts the bits from the high bit to the low bit. The << operator shifts the bits from the low bit to the high bit. One use for these operators is to align the bits for whatever reason.

not_ex.JPG

Bit Fields

The most convenient and fast method to represent a certain information is a
multiple of bytes, even if there are unused bits.

We can use in an efficient manner the memory (in detriment of speed) using packed data.This means the representation of the desired information on the minimum number of possible bits.
 

bit_fields.jpg

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

Silviu Caragea

Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject.

He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy.

His programming experience includes:
- C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL)
- Open Source libraries :CURL & Boost
- HTML, CSS
- Java (SE,ME)
- JavaScript, Ajax, Google Web Toolkit (GWT)
- Php, MySQL
-Oracle, PL SQL
- C# .NET
-Objective C, IPhone SDK, Cocoa

Location: Romania
Ocupation: Software Engineer
Home page: http://www.intelliproject.net

Sign up to post message on the article message board!