1. 程式人生 > >C++之Basic knowledge

C++之Basic knowledge

alt actual stand api header nis efi vat mes

目錄

  • Ascii Codes
  • Boolean Operations
    • AND
    • OR
    • XOR (Exclusive Or)
    • NOT
  • Numerical Bases
    • Octal numbers (base 8)
    • Hexadecimal numbers (base 16)
    • Binary representations
  • Reference websites

Ascii Codes


It is a very well-known fact that computers can manage internally only 0s (zeros) and 1s (ones). This is true, and by means of sequences of 0s and 1s the computer can express any numerical value as its binary translation, which is a very simple mathematical operation (as explained in the paper numerical bases).

Nevertheless, there is no such evident way to represent letters and other non-numeric characters with 0s and 1s. Therefore, in order to do that, computers use ASCII tables, which are tables or lists that contain all the letters in the roman alphabet plus some additional characters. In these tables each character is always represented by the same order number. For example, the ASCII code for the capital letter "A" is always represented by the order number 65, which is easily representable using 0s and 1s in binary: 65 expressed as a binary number is 1000001.

The standard ASCII table defines 128 character codes (from 0 to 127), of which, the first 32 are control codes (non-printable), and the remaining 96 character codes are representable characters:

* 0 1 2 3 4 5 6 7 8 9 A B C D E F
0 NUL SOH STX ETX EOT ENQ ACK BEL BS TAB LF VT FF CR SO SI
1 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
2 ! " # $ % & ( ) * + , - . /
3 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4 @ A B C D E F G H I J K L M N O
5 P Q R S T U V W X Y Z [ |] ^ _
6 ` a b c d e f g h i j k l m n o
7 p q r s t u v w x y z { } ~

* This panel is organized to be easily read in hexadecimal: row numbers represent the first digit and the column numbers represent the second one. For example, the "A" character is located at the 4th row and the 1st column, for that it would be represented in hexadecimal as 0x41 (65).

Because most systems nowadays work with 8bit bytes, which can represent 256 different values, in addition to the 128 standard ASCII codes there are other 128 that are known as extended ASCII, which are platform- and locale-dependent. So there is more than one extended ASCII character set.

The two most used extended ASCII character sets are the one known as OEM, that comes from the default character set incorporated by default in the IBM-PC and the other is the ANSI extend ASCII which is used by most recent operating systems.

The first of them, the OEM character set, is the one used by the hardware of the immense majority of PC compatible machines, and was also used under the old DOS system. It includes some foreign signs, some marked characters and pieces to represent panels.

技術分享圖片
The ANSI character set is a standard that many systems incorporate, like Windows, some UNIX platforms and many standalone applications. It includes many more local symbols and marked letters so that it can be used with no need of being redefined in many more languages:

技術分享圖片

--------------------------------------------------

Boolean Operations


A bit is the minimum amount of information that we can imagine, since it only stores either value 1 or 0, which represents either YES or NO, activated or deactivated, true or false, etc... that is: two possible states each one opposite to the other, without possibility of any shades. We are going to consider that the two possible values of a bit are 0 and 1.

Several operations can be performed with bits, either in conjunction with other bits or themselves alone. These operations receive the name of boolean operations, a word that comes from the name of one of the mathematicians who contributed the more to this field: George Boole (1815-1864).

All these operations have an established behavior and all of them can be applied to any bit no matter which value they contain (either 0 or 1). Next you have a list of the basic boolean operations and a table with the behavior of that operation with every possible combination of bits.

AND


This operation is performed between two bits, which we will call a and b. The result of applying this AND operation is 1 if both a and b are equal to 1, and 0 in all other cases (i.e., if one or both of the variables is 0).

AND (&)
a | b | a&b
-|-|-
0|0|0
0|1|0
1|0|0
1|1|1

OR


This operation is performed between two bits (a and b). The result is 1 if either one of the two bits is 1, or if both are 1. If none is equal to 1 the result is 0.

OR (|)
a|b|a|b
-|-|-
0|0|0
0|1|1
1|0|1
1|1|1

XOR (Exclusive Or)


This operation is performed between two bits (a and b). The result is 1 if either one of the two bits is 1, but not in the case that both are. There for, if neither or both of them are equal to 1 the result is 0.

XOR (^)
a|b|a^b
-|-|-
0|0|0
0|1|1
1|0|1
1|1|0

NOT


This operation is performed on a single bit. Its result is the inversion of the actual value of the bit: if it was set to 1 it becomes 0, and if it was 0 it becomes 1:

NOT (~)
a|~a
-|-
0|1
1|0

These are the 4 basic boolean operations (AND, OR, XOR and NOT). Combining these operations we can obtain any possible result from two bits.

In C++, these operators can be used with variables of any integer data type; the boolean operation is performed to all of the bits of each variable involved. For example, supposing two variables: a and b, both of type unsigned char, where acontains 195 (11000011 in binary) and b contains 87 (or 01010111 in binary). If we write the following code:

unsigned char a=195;
unsigned char b=87;
unsigned char c;
c=a&b;

That means, that we conducted a bitwise AND operation between a and b. The operation is performed between the bits of the two variables that are located at the same position: The rightmost bit of c will contain the result of conducting the AND operation between the rightmost bits of a and b:

技術分享圖片

The same operation is also performed between the second bits of both variables, and the third, and so on, until the operation is performed between all bits of both variables (each one only with the same bit of the other variable).

The final binary value of c is 01000011, that is 67 in decimal numbers. So 195&87 is equal to 67.

----------------------------------------------------------------------

Numerical Bases


Since we were kids, we have all used decimals to express quantities. This nomenclature that seems so logical to us may not seem so to an inhabitant of Classical Rome. For them, each symbol that they wrote to express a number always represented the same value:

I 1
II 2
III 3
IV 4
V 5

All the I signs always represents the value 1 (one) wherever they are placed, and the V sign always represents a value of 5 (five). Nevertheless that does not take place in our decimal system. When we write the decimal symbol 1 we are not always talking about a value of one (I in Roman numbers). For example:

1 I
10 X
100 C

In these cases, our symbol 1 does not have always a value of one (or I in Roman numbers). For example, in the second case, the symbol 1 represents a value of ten (or X in Roman) and in the third one, 1 represents a value of one hundred (or C).

For example:

275 is not equivalent to 2+7+5, it could rather be decomposed as 200+70+5:

200
+ 70
5
---
275

therefore, the first "2" sign is equivalent to 200 (2 x 100), the second "7" sign is equivalent to 70 (7 x 10) whereas the last sign corresponds to the value 5 (5 x 1).

This is because our system is a positional numeral system. Therefore, the value of a given digit depends on its position within the entire number being represented. All the above can be mathematically represented in a very simple way. For example, to represent the value 182736 we can assume that each digit is the product of itself multiplied by 10 powered to its place as exponent, beginning from the right with 100, following with 101, 102, and so on:

技術分享圖片

Octal numbers (base 8)


Like our "normal" numbers are base 10 (or radix 10) because we have 10 different digits (from the 0 to the 9):

0123456789

the octals numbers include only the representations for the values from 0 to 7:

01234567

and, therefore, its mathematical base is 8. In C++ octal numbers are denoted by beginning always with a 0 digit. Let‘s see how we would write the first numbers in octal:

octal decimal
----- -------
0 0 (zero)
01 1 (one)
02 2 (two)
03 3 (three)
04 4 (four)
05 5 (five)
06 6 (six)
07 7 (seven)
010 8 (eight)
011 9 (nine)
012 10 (ten)
013 11 (eleven)
014 12 (twelve)
015 13 (thirteen)
016 14 (fourteen)
017 15 (fifteen)
020 16 (sixteen)
021 17 (seventeen)

Thus, for example, the number 17 (seventeen, or XVII in Roman) it is expressed 021 as an octal number in C++. We can apply the same mechanism that we saw previously for decimal numbers to the octal numbers simply by considering that its base is 8. For example, taking the octal number 071263:

技術分享圖片
therefore the octal number 071263 is expressed as 29363 in decimal numbers.

Hexadecimal numbers (base 16)


Like decimal numbers have 10 different digits to be represented (0123456789) and octal numbers have 8 (01234567), hexadecimal numbers have 16 different digits, that are represented by the numbers from 0 to 9 and the letters A, B, C, D, E and F, which together serve us to represent the 16 different symbols that we need to express base 16 numbers:

hexadecimal decimal
----------- -------
0 0 (zero)
0x1 1 (one)
0x2 2 (two)
0x3 3 (three)
0x4 4 (four)
0x5 5 (five)
0x6 6 (six)
0x7 7 (seven)
0x8 8 (eight)
0x9 9 (nine)
0xA 10 (ten)
0xB 11 (eleven)
0xC 12 (twelve)
0xD 13 (thirteen)
0xE 14 (fourteen)
0xF 15 (fifteen)
0x10 16 (sixteen)
0x11 17 (seventeen)

In C++, hexadecimal numbers are preceded by 0x (zero, x).

Once again we can use the same method to translate a number from a base to another one:

技術分享圖片

Binary representations


Octal and hexadecimal numbers have a considerable advantage over our decimal numbers in the world of bits, and is that their bases (8 and 16) are perfect multiples of 2 (23 and 24, respectively), which allows us to make easier conversions from these bases to binary than from decimal numbers (whose base is 2x5). For example, suppose that we want to translate the following binary sequence to numbers of other bases:

110011111010010100

In order to translate it to decimal we would need to conduct a mathematical operation similar to the one we have used previously to convert from hexadecimal or octal, which would give us the decimal number 212628.

Nevertheless to pass this sequence to octal it will only take us some seconds and even the less skilled in mathematics can do it just by seeing it: Since 8 is 23, we will separate the binary value in groups of 3 numbers:

110 011 111 010 010 100

and now we just have to translate to octal numberal radix each group separately:

110 011 111 010 010 100
6 3 7 2 2 4

giving the number 637224 as result. This same process can be inversely performed to pass from octal to binary.
In order to conduct the operation with hexadecimal numbers we only have to perform the same process but separating the binary value in groups of 4 numbers, because 16 = 24:

11 0011 1110 1001 0100
3 3 E 9 4

Therefore, the binary expression 110011111010010100 can be represented in C++ either as 212628 (decimal), as 0637224 (octal) or as 0x33e94 (hexadecimal).

The hexadecimal code is specially interesting in computer science since nowadays, computers are based on bytes composed of 8 binary bits and therefore each byte matches with the range that 2 hexadecimal numbers can represent. For that reason it is so frequently used to represent values translated to or from binary base

Reference websites

[1] Ascii
[2] Boolean
[3] Hex

  • 筆記變更記錄
時間 地點 修改人 備註
2018-10-04 xuzhou ligure

C++之Basic knowledge