Type of bool for OC

Source: Internet
Author: User

Type of bool for OC:

The bool type is actually added by a mechanism called a preprocessor. typedef signed Char BOOL;

The bool type has two values of yes,no. Yes=1,no=0.

Description: BOOL in objective-c is actually a type definition (typedef) of a signed character type (signed char) that uses 8 bits of storage space. Define yes with the # define directive as 1,no defined as 0.

Note: OBJECTIVE-C does not handle bool as a true Boolean type that can only hold yes or no values. The compiler still views bool as a 8-bit binary number, and YES and NO values are just a habit of understanding.

Problem: If you accidentally assign an integer value greater than 1 bytes (such as short or int) to a bool variable, then only the low byte is used as the bool value. If the low-order byte is exactly 0 (for example, 8960, written as 16 0x2300), the bool value will be considered as 0, or no value. for bool type, only the difference between true and false, that is, 0 is false, and not 0 is true. The bool type variable returns 1 if the value of the variable is 0, or 0 returns 0.

Example:

BOOL b1=8960; Actually is no, because 8960 hexadecimal for 0x2300,bool only 8 bits of storage space, take 0x2300 low 8 bit, 00, so is no

BOOL b2=8960;//is actually true because the bool type, not 0, is true.

Test:

BOOL j = 8960;

NSLog (@ "j:%d", j);//2015-01-28 22:40:29.935 address function as a parameter [3468:251458] j:0

bool m = 8960;

NSLog (@ "m:%d", m),//2015-01-28 22:40:29.935 address function as a parameter [3468:251458] M:1

Related knowledge:

① machine Word length: Refers to the number of bits of binary data that a computer can handle in a single integer operation (integer operations are fixed-point integer operations). It is usually the width of the CPU's internal data path. That is, the longer the word length, the larger the range of the numbers, the higher the accuracy. The machine's word length also affects the machine's operating speed. If the CPU length is short, but also the number of operations more data, then need to go through two or more operations to complete, which will inevitably affect the speed of the whole machine. Machine Word length is usually the same as the main memory word length, but it can also be different. In different cases, the main memory is generally less than the length of the machine word length, for example, the machine word length is 32 bits, the main memory word can be 32 bits, or 16 bits, of course, both will affect the efficiency of the CPU.

② in a computer system, values are expressed and stored in complement.

③ The original code notation adds a sign bit (that is, the highest bit to the sign bit) before the value: a positive number is 0, a negative number is 1 (0 has two representations: +0 and 0), and the remaining bits represent the size of the value. There are many 8-bit binary primitives in the representation mode. So the original code that says a number out of representation is bullying. In addition, the complement is the original code.

EG (with 8-bit binary notation), 5:0000 0101. 28:0001 1100. -13:1000 1101

The inverse code notation stipulates that the inverse code of a positive number is the same as its original code, and the inverse of a negative number is a bitwise negation of its original code, except for the sign bit.

The complement notation stipulates that the complement of positive numbers is the same as the original code, and that the complement of negative numbers is added to the minus 1 of the inverse code.

The complement is represented in many ways, with a 8-bit twos complement representation, a 16-bit twos complement representation, and a 32-bit twos complement representation, a 64-bit complement representation, and so on. Each complement representation can only represent a limited number.

The ④ complement and the original code of the mutual conversion. It is known that the complement of a number, the operation of the original code is to complement the complement: ⑴ if the sign of the complement is "0", the expression is a positive, its original code is the complement.

⑵ if the sign of the complement is "1", indicating a negative number, then the complement of the given complement is the required source code.

"Example" is known as a complement of 11111001, the original code is 10000111 (-7).

Because the sign bit is "1", it indicates a negative number, so the bit is unchanged and remains "1". The remaining seven digits 1111001 are reversed to 0000110; plus 1, so 10000111.

So:

BOOL L = 130; 130 of the 16-bit binary source code is: 0000 0000 1000 0010. Because in a computer, values are stored in the form of a complement. So convert the original code into a complement. The complement of a positive conversion rule is: 0000 0000 1000 0010. Use the bool type to load the number. Since BOOL is a signed character type that accounts for only 8 bits, the 8 bits from the last intercept are: 1000 0010. Converting to the original code and printing it in decimal becomes-126. The conversion process (because it is a signed character type, so the highest bit represents the symbol bit, so the highest bit remains the same, the remaining digits are reversed plus 1): 1111 1101 + 1 = 1111 1110---126

NSLog (@ "l:%d", l);//2015-01-28 22:15:11.249 address function as a parameter [3440:242113] l:-126

BOOL k = -130;//1000 0000 1000 0010 turn to complement 1111 1111 0111 1110, then intercept 8 bits into 0111 1110, because it is signed, so the highest bit represents the sign bit, 0 is a positive number, there is a rule that the original code of positive number is the complement, A positive complement is the original code. Thus obtaining its original code is: 0111 1110, converted to decimal is 126.

NSLog (@ "k:%d", K),//2015-01-28 22:15:11.249 address function as a parameter [3440:242113] k:126

Type of bool for OC

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.