In-depth understanding of computer systems (2.2)---Boolean algebra and C-language bitwise operations

Source: Internet
Author: User
Tags bitwise

This article reprinted address : http://www.cnblogs.com/zuoxiaolong/p/computer6.html

Bitwise operations on Boolean algebra

Boolean algebra is a mathematical knowledge system that evolves from binary values of 0 and 1.

We do not need to thoroughly understand this knowledge system, but in the definition of several binary operations, but we also encountered in the normal programming process. These four operations are or, are, are not and XOR . The definition of these four kinds of operations is presented in the knowledge System of Boolean algebra.

From left to right in turn is non-, and, or, and XOR. This diagram illustrates the result of a binary operation, which we can extend to n-bit binary. For example, two binary [aw,aw-1...a1] and [BW,BW-1...B1], their four operations are to each of the two corresponding bits of the appropriate operation.

That is to say, if the result is [CW,CW-1...C1], then for any ci to meet CI = AI (|,&,^) Bi, if the [aw,aw-1...a1] non-operation, then CI = ~ai.

Bit arithmetic in C language

In the C language, bit operations are also supported, and it is calculated as bit operations in Boolean algebra. Bit operations are the most common way we use masks. For example, we know an integer x, and if we want to get the integer value of the last byte of this integer, we can take a bitwise operation. Just like this.

#include <stdio.h>int  main () {     int0x12345678;      int 0xFF ;      int k = i & j;     printf ("%x\n", k);}

Eventually we want the result to be 78, which is the value of the last byte of the integer I, and we use a mask of 0xFF and three bytes to filter out the high of the integer I with the operation. Here are the results.

logical operation of C language

The logical operations in C language are |, && and!, which is easier to mix with just |,& and. The logical operation is particularly, in the result of this operation that all values other than 0 are true, and 0 is false. LZ here wrote a small program, we come to a simple look at the difference between the two.

#include <stdio.h>intMain () {unsignedintx =0x12345678; unsignedinti =!x; unsignedintj = ~x; unsignedintm =!!x; unsignedintn = ~ ~x; printf ("%u%u\n", i,j); printf ("%u%u\n", m,n);}

From the results of this program can be clearly seen! And the difference between the operation, we have to see the results directly.

The result is obvious, the left side is!x and!! The result of X is only 0 and 1, and the right side is the result of ~x and ~~x. It is obvious that the difference between the logical non-bitwise and the non-operation. The former is only 0 and 1, while the latter is the inverse of each bit binary value.

Apart from the differences in results, there is one difference between them, that is, if the logical operation can determine the result based on the first expression, then the second expression will not be evaluated. For a simple example, suppose there are two expressions A and B, and for a && B, if A is false, the value of the B expression is not computed. For A & B, however, the value of the B expression is evaluated regardless of the value of the a expression.

Shift operation in C language

There are two types of shift operations, left and right shift . For a binary number [aw,aw-1,... A1], if it is left-shifted, x << k = [aw-k,aw-k-1,... a1,0,... 0]. At this time the equivalent of the highest K-bit is discarded, at the right end of the complement of K 0. For x >> K, that is, the right-shift operation is similar to the left shift, except that in order to take care of the signed number, it is sometimes necessary to fill the top of the leftmost instead of 0. for the case of 0, it is called logical right SHIFT, and the highest bit is called arithmetic right shift .

That is, for the logical right SHIFT, x >> k = [0,... 0,aw,aw-1,... Ak+1], and for arithmetic right shift, x >> k = [aw,... aw,aw,aw-1,... ak+1].

Summary of this chapter

This chapter mainly simply introduces the bit operations in the C language, and the next chapter will describe how integers are represented.

In-depth understanding of computer systems (2.2)---Boolean algebra and C-language bitwise operations

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.