Traffic light problem (bit operation)

Source: Internet
Author: User
Consider a 10-character intersection. Each intersection has three traffic lights, indicating whether to turn left, turn right, and go straight. There are a total of 12, and now we need to write a control for itProgramRegardless of the function of the program, you must first convert the traffic light status to a number, because the computer only knows the number. Therefore, three numbers are used to represent the three traffic lights at an intersection. Therefore, the status of each traffic light is represented by a number. If the red light is 0, the green light is 1 (do not consider the yellow light or other circumstances ).
Later, I suddenly found that, in fact, I could also use a number to represent the status of three traffic lights at an intersection. For example, I would use 110 to represent a left-turn green light, a straight green light, and a right-turn red light. The above 110 is a decimal number, and every bit of it can actually be 0 ~ It is a waste of nine digits, but only two digits: 0 and 1 are used here. Therefore, if the binary number is used, it is still 110, but it is a binary number. If it is converted to 6, it is only 7 even if it is 111, it is much smaller than the decimal number above 110, saving ......?? What ??
Let's continue with the above thinking. The binary number 110 is used to indicate the status of the three traffic lights. Now you need to know what status the number 110 represents. The third digit represents the left turn, but the computer does not know this, so the following is: 110 & 100. The return value of this expression is 100, non-zero, and the logic is true. Assume that the status of an intersection is 010, then the same 010 & 100, the return value is 0, and the logic is false. Therefore, the "&" operator can be used to extract the status of one or several digits in the binary number. So when we want to know whether a number indicates a left-turn traffic light in the traffic light status is a green light, we only need to make it and the 100 phase.
Now we need to keep the status of other traffic lights unchanged. We only need to set the left turn traffic light to the green light. For example, the current status is 010. To set the left turn traffic light to the green light, the value should be 110, this can be done through 010 | 100. If the current status is 001, 001 | 100 is 101. correct: the traffic light status of the direct and right directions does not change. Therefore, you can use the "|" operator to set the status of one or more digits of a binary number, but only 1. If you want to set it to 0, such as 101, to turn off the green light of the left turn, 101 &~ 100. The return value is 001.
The preceding mentioned intersection traffic light status can be represented by a variable in actual compiling, and the above 100 can also be represented by an identifier, such as State & ts_left, it can be used to check the status of the Left turn traffic light in the State indicated by the variable state.
The above method is widely used. For example, if you create a window and a window may have 20 or 30 styles, you can use the above method, you can use only a 32-bit binary number to represent the window style, instead of having to get 20 or 30 numbers to indicate whether each style has either.

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.