[Translation] C # data structures and algorithms-Chapter 6 BitArray

Source: Internet
Author: User
Tags bit set

The BitArray class is used to represent a series of bit values when resources are limited. Bit sets can be stored in regular arrays, but if we use a data structure designed specifically for bit sets, we can create more efficient programs. In this chapter, we will learn how to use this data structure and explore some problems that can be solved using bit sets. This chapter also reviews binary numbers and bitwise comparison and shift operators.

A question that aroused interest

Let's look at a problem that will eventually be solved using the BitArray class. The requirement for this problem is to find the prime number. A more primitive method was discovered by the Greek philosopher heratoshny in the third century BC, known as the irantos siscreen method. This method filters out the numbers that can be divisible by other numbers until the remaining numbers are prime numbers. For example, let's find the prime number in the first 100 integers. We start with the first prime number 2. We traverse the set to remove the multiples of all 2. Then we proceed to the next Prime Number 3. We traverse the set again and remove the multiples of all three. Then we move to 5, so proceed. When we finish, all the remaining numbers are prime numbers.

First, we use a regular array to solve this problem. The method we will use is similar to the method to solve the problem using a BitArray. It initializes the 100 elements of the array and sets the value of each element to 1. Starting from index 2 (because 2 is the first prime number), each subsequent array index is first viewed as 1 or 0. If the value is 1, then determine whether the number of indexes is a multiple of 2. If yes, set the index value to 0. Then we move to index 3 to perform the same processing and continue.

To write code to solve this problem, we will use the Carray class we wrote earlier. The first thing to do is to create a method for filtering. The following code is used:

Public void GenPrimes ()

{

Int temp;

For (int outer = 2; outer <= arr. GetUpperBound (0); outer ++)

For (int inner = outer + 1; inner <= GetUpperBound (0); inner ++)

If (arr [inner] = 1)

If (inner % outer) = 0)

Arr [inner] = 0;

}

Now we only need to display the prime number:

Public void ShowPrimes ()

{

For (int I = 2; I <= arr. GetUpperBound (0); I ++)

If (arr [I] = 1)

Console. Write (I + "");

}

Below is a program to test our code:

Static void Main ()

{

Int size = 100;

CArray primes = new CArray (size-1 );

For (int I = 0; I <= size-1; I ++)

Primes. Insert (1 );

Primes. GenPrimes ();

Primes. ShowPrimes ();

}

This method shows how to use the irantos screening method in an integer array, but it is better to use bits to develop a solution, because each element in this array is 1 or 0. Later in this chapter, we will learn how to use the BitArray class to simultaneously implement the ilatosen screening method and other questions about how to lend yourself to the bit set.

Bitwise AND bitwise operations

Before studying the BitArray class, we need to study how to use bits in VB. NET, because the first-level operations in place are not familiar to most VB. NET programmers. In this section, we will learn how to operate bits in VB. NET by learning how to operate bits using bitwise operators.

Binary counting system

Before learning to operate bits, let's review the binary counting system. Binary numbers use strings 0 and 1 to represent 10-based (decimal) numbers with 2-based numbers. For example, the integer 0 in the binary is as follows:

00000000

The binary number of integer 1 is:

00000001

The following format is displayed as an integer 0-9 in binary:

Commandid -0d (where d signifies a decimal number)

20170001-1d

20170010-2d

20170011-3d

00000100-4d

00000101-5d

00000110-6d

00000111-7d

20171000-8d

20171001-9d

The best way to convert a binary number to an equivalent decimal is to use the following scheme. In each binary number, the rightmost number represents a power of 2 that increases continuously. If the number at the first position is 1, it indicates 20. If there is a 1 in the second position, it indicates 21, and so on.

Binary Number:

00101010

It is equivalent:

0 + 21 + 0 + 23 + 0 + 25 + 0 + 0 = 0 + 2 + 0 + 8 + 0 + 32 + 0 + 0 = 42

The bit is usually displayed in a collection of eight digits, which constitutes a byte. The maximum number that can be expressed with 8 digits is 255, and the binary value is as follows:

11111111

Or

1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255

A number greater than 255 must be stored in 16 bits. For example, the binary number 256 indicates:

00000001 00000000

Although not required, it is used to separating the Lower 8 bits from the higher 8 bits.

Operation binary number: bitwise operation and shift operator

Binary numbers do not use basic arithmetic operators. You must use the bitwise operator (And, Or, Not) Or the offset operator (<, >>, and >> ). In this section, we will explain how these operators work and demonstrate their usage in VB. NET programs in the following sections.

First, we study bitwise operators. These operators, which are familiar to most programmers, are used to merge related expressions to calculate a single Boolean value. Bitwise operators operate on binary numbers, compare two binary numbers by bit, and generate a new binary number iteratively.

Bitwise operators work in the same way as boolean values. When a binary number is operated, the True bits are equivalent to 1, and the False bits are equivalent to 0. We use a truth table to demonstrate bitwise operator operations, just like the truth table used for Boolean operations. The first two columns in a row are the operands, and the third column is the calculation result. The And operation truth table (Boolean) is as follows:

True

True

True

True

False

False

False

True

False

False

False

False

Truth Table of equal bit values:

1

1

1

1

0

0

0

1

0

0

0

0

Or:

True

True

True

True

False

True

False

True

True

False

False

False

Truth Table of equal bit values:

1

1

1

1

0

1

0

1

1

0

0

0

The last is the Xor operator. This is the least known bitwise operator because it is not used for logical operations executed by computer programs. When two bits are compared with Xor, when one of the two bits is 1, the result is 1. The following is a truth table:

1

1

0

1

0

1

0

1

1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.