| |

VerySource

 Forgot password?
 Register
Search
View: 935|Reply: 6

Ask a bit arithmetic problem!

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-16 15:40:01
| Show all posts |Read mode
Fill in a blank question and fill in the results:
int x;
(x&1) + (x1)-(x% 2) + (~ (~ 0U << 2)) = ______;
Ask the master what the result is, what is the detailed calculation process?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-21 15:00:02
| Show all posts
int x = 0; // There is no initialization x, the discussion is meaningless, so the following discussion assumes that x is initialized to 0

(x&1) == 0 according to logical AND
(x | 1) == 1 according to logical OR
(x% 2) == 0 is equivalent to (x&1)
(~ (~ 0U << 2)) == 3 0 is inverted, all bits are 1, and then shifted to the left by two bits, then the next two bits are 0, and then inverted, then the first 1 is all 0, the latter Get two 1s, converted to decimal 3
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-21 16:00:02
| Show all posts
The x of the title is not assigned, but the stable result can be obtained by debugging with tc. The result is output as 914 as% d, and what does the U in the last registration mean?
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-10 17:45:01
| Show all posts
U is unsigned
Reply

Use magic Report

0

Threads

1

Posts

0.00

Credits

Newbie

Rank: 1

Credits
0.00

 China

Post time: 2020-3-10 23:23:10
| Show all posts
It's so complicated. It's the interview. There will be abnormal questions in the interview.
Reply

Use magic Report

0

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-3-21 19:45:02
| Show all posts
This question is meaningless, x is not initialized and will be given a random value depending on the compilation environment
The result is (x | 1) +3, your so-called stable result is just given by your environment
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-2 09:00:02
| Show all posts
Consensus with LS.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list