Please explain the meaning of the above line, and in particular, why such an iteration "t = t / 2" is used.
PS If possible, advise the literature so that more such silly questions do not arise :)
- As I understand it, this is Herbert Shildt? - Vennic
- Welcome to stack overflow in Russian! text information is better to attach as text: a) easier to read; b) can be copied; c) the search works. You can correct the question text by clicking below to edit the question text - aleksandr barakin
- Yes, absolutely for a long time, I was thinking of starting either from Serie and Bates studying java or from a shieldt, I came to the conclusion that a shieldt is better - DD DD
2 answers
In short, this operation shifts the unit bit in the mask one position to the right.
Equivalent records:
t >> 1 If more, then the number 128 in binary number system:
10000000 If 128 is divided in half, we get 64, which is in binary notation:
01000000 32 will match:
00100000 And so on, the single bit will move to the most rightmost position.
What is it for?
The fact is that the loop uses bitwise & , and at each iteration we sequentially get a bit from the variable val . As a result, if a positive number is obtained, then there is a one in this position, otherwise 0. If you don’t understand why this is the case, I recommend reading how bitwise operations work
- Thank you, your answer helped me to understand, but you could not advise me what to read to understand the number systems, otherwise you can say that I began to study Java from scratch, that is, without any knowledge of computer science - DD DD
- @ DDD I cannot advise any specific literature, but there are enough resources on the Internet on which the topic of number systems is revealed (definitions, translation from one to another, arithmetic, etc.). I would like to note that this is a common basic theme, not tied specifically to the Java programming language. I would advise you now to develop without a specific link to PL, and to gain knowledge in the field of computer science in general. For example, move towards data structures and algorithms. Programming languages are born and die, and data structures and algorithms have been used for decades. - Roman Danilov
- Only
t >> 2is divided by 4, and in half it ist >> 1- avp - @avp, yes, indeed, corrected a slip of the pen - Roman Danilov
Because the binary number system is a power of two. Pay attention to the bitwise operation &, you can paint this cycle as:
// 0 1 1 1 1 0 1 1 // 1 0 0 0 0 0 0 0 (128) // & // 0 1 1 1 1 0 1 1 (123) // 0 0 0 0 0 0 0 0 0 // 0 1 0 0 0 0 0 0 (64) // & // 0 1 1 1 1 0 1 1 (123) // 0 1 0 0 0 0 0 0 1 // 0 0 1 0 0 0 0 0 (32) // & // 0 1 1 1 1 0 1 1 (123) // 0 0 1 0 0 0 0 0 1 // 0 0 0 1 0 0 0 0 (16) // & // 0 1 1 1 1 0 1 1 (123) // 0 0 0 1 0 0 1 0 1 // 0 0 0 0 1 0 0 0 (8) // & // 0 1 1 1 1 0 1 1 (123) // 0 0 0 0 1 0 0 0 1 // 0 0 0 0 0 1 0 0 (4) // & // 0 1 1 1 1 0 1 1 (123) // 0 0 0 0 0 0 0 0 0 // 0 0 0 0 0 0 1 0 (2) // & // 0 1 1 1 1 0 1 1 (123) // 0 0 0 0 0 0 1 0 1 // 0 0 0 0 0 0 0 1 (1) // & // 0 1 1 1 1 0 1 1 (123) // 0 0 0 0 0 0 0 1 1 So for the transfer back you will need to do the following:
0 1 1 1 1 0 1 1 (0*2^7) + (1*2^6) + (1*2^5) + (1*2^4) + (1*2^3) + (0*2^2) + (1*2^1) + (1*2^0) 0 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = 123