|
Location: Computer science - Algorithms License: The Intelliproject Open License (IPOL) An introduction to bitwise operatorsPosted by Silviu CarageaThis article gives a brief overview of C style bitwise operators |
Skill: BeginnerPosted: 04/10/2008Views: 424Rating: 5.00 /5Popularity: 0.00 |
| Sign Up to vote for this article |
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.

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.

There are six bitwise operators:
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:
Far, most uses of the XOR are based on the second proprietary:
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.
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.

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)
| 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: |
Sign up to post message on the article message board!