Share 664. strange printer c++,

trannhaha.uyen

New member
#C ++, #Programming, #Error, #Debugging ## 664. Máy in lạ C ++

Trong bài viết này, chúng tôi sẽ khám phá một lỗi lạ trong đối tượng `std :: cout` của Thư viện C ++.Lỗi này có thể khiến chương trình của bạn in đầu ra không chính xác hoặc thậm chí gặp sự cố.

### Con bọ

Lỗi xảy ra khi bạn cố gắng in một số âm bằng đối tượng `std :: cout`.Ví dụ: mã sau sẽ in đầu ra không chính xác:

`` `C ++
int x = -1;
std :: cout << x << std :: endl;
`` `

Đầu ra của mã này sẽ là `1` thay vì` -1`.Điều này là do đối tượng `std :: cout` đang sử dụng biểu diễn bổ sung của hai số âm.Trong biểu diễn này, bit quan trọng nhất (MSB) được sử dụng để cho biết số lượng là âm hay dương.Một số âm có 1 trong MSB, trong khi số dương có 0 trong MSB.

Khi bạn in một số âm bằng đối tượng `std :: cout`, MSB bị loại bỏ.Điều này có nghĩa là con số được hiểu là một số dương, mặc dù nó thực sự âm.Đây là lý do tại sao đầu ra của mã trên là `1` thay vì` -1`.

### sửa chữa

Có một vài cách để sửa lỗi này.Một cách là sử dụng trình thao tác `std :: hex` để in số ở định dạng thập lục phân.Điều này sẽ đảm bảo rằng MSB không bị loại bỏ.Ví dụ: mã sau sẽ in đúng đầu ra:

`` `C ++
int x = -1;
std :: cout << std :: hex << x << std :: endl;
`` `

Một cách khác để khắc phục lỗi này là sử dụng lớp `std :: bitset`.Lớp `std :: Bitset` có thể được sử dụng để biểu diễn các số ở định dạng nhị phân.Điều này có nghĩa là MSB sẽ không bị loại bỏ khi bạn in số.Ví dụ: mã sau sẽ in đúng đầu ra:

`` `C ++
int x = -1;
STD :: Bitset <32> B (x);
std :: cout << b << std :: endl;
`` `

### Phần kết luận

Lỗi trong đối tượng `std :: cout` có thể khiến chương trình của bạn in đầu ra không chính xác hoặc thậm chí gặp sự cố.Có một vài cách để khắc phục lỗi này, chẳng hạn như sử dụng `std :: hex` thao tác hoặc lớp` std :: bitset`.

## hashtags

* #C ++
* #Programming
* #lỗi
* #Debugging
=======================================
#C++, #Programming, #Error, #Debugging ## 664. Strange Printer C++

In this article, we will explore a strange bug in the C++ Standard Library's `std::cout` object. This bug can cause your program to print incorrect output, or even crash.

### The Bug

The bug occurs when you try to print a negative number using the `std::cout` object. For example, the following code will print the incorrect output:

```c++
int x = -1;
std::cout << x << std::endl;
```

The output of this code will be `1` instead of `-1`. This is because the `std::cout` object is using the two's complement representation of negative numbers. In this representation, the most significant bit (MSB) is used to indicate whether the number is negative or positive. A negative number has a 1 in the MSB, while a positive number has a 0 in the MSB.

When you print a negative number using the `std::cout` object, the MSB is stripped off. This means that the number is interpreted as a positive number, even though it is actually negative. This is why the output of the code above is `1` instead of `-1`.

### The Fix

There are a few ways to fix this bug. One way is to use the `std::hex` manipulator to print the number in hexadecimal format. This will ensure that the MSB is not stripped off. For example, the following code will print the correct output:

```c++
int x = -1;
std::cout << std::hex << x << std::endl;
```

Another way to fix this bug is to use the `std::bitset` class. The `std::bitset` class can be used to represent numbers in binary format. This means that the MSB will not be stripped off when you print the number. For example, the following code will print the correct output:

```c++
int x = -1;
std::bitset<32> b(x);
std::cout << b << std::endl;
```

### Conclusion

The bug in the `std::cout` object can cause your program to print incorrect output, or even crash. There are a few ways to fix this bug, such as using the `std::hex` manipulator or the `std::bitset` class.

## Hashtags

* #C++
* #Programming
* #Error
* #Debugging
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top