Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Sử dụng kiểm tra và đánh dấu Integer số học Trong chương 2, bạn đã học cách sử dụng toán tử số học nhị phân chẳng hạn như + và * trên dữ liệu các loại nguyên thủy như int và tăng gấp đôi. Bạn cũng thấy rằng các loại dữ liệu nguyên thủy có kích thước cố định. Ví dụ, một C # int là 32 bit. | Using Checked and Unchecked Integer Arithmetic In Chapter 2 you learned how to use binary arithmetic operators such as and on primitive data types such as int and double. You also saw that the primitive data types have a fixed size. For example a C int is 32 bits. Because int has a fixed size you know exactly the range of value that it can hold it is -2147483648 to 2147483647. TIP If you want to determine the minimum or maximum value of int in code you can use the Int32.MinValue or Int32.MaxValue fields. The fixed size of the int type creates a problem. For example what happens if you add 1 to an int whose value is currently 2147483647 The answer is that it depends on how the application is compiled. By default the C compiler generates code that allows the calculation to silently overflow. In other words you get the wrong answer. In fact the calculation wraps around to the largest negative integer value and the result generated is -2147483648. The reason for this behavior is performance integer arithmetic is a common operation in almost every program and adding the overhead of overflow checking to each integer expression could lead to very poor performance. In many cases the risk is acceptable because you know or hope that your int values won t reach their limits. If you don t like this approach you can turn on overflow checking by setting. TIP You can enable and disable overflow checking in Visual Studio 2005 by setting the project properties. On the Project menu click YourProject Properties where YourProject is the name of your project . In the project properties dialog box click the Build tab. Click the Advanced button in the lower-right corner of the page. In the Advanced Build Settings dialog box select or clear the Check for arithmetic overflow underflow check box. Regardless of how you compile an application you can use the checked and unchecked keywords to selectively turn on and off integer arithmetic overflow checking in parts of an application that you .