I asked this question on Stackoverflow and they seemed to have answered it in great detail, but unfortunately my English is not enough to deal with the tricky bit magic of Java. if it is not difficult, then somebody could explain in general in Russian what and how and where. very grateful
- onelearn English to begin with, at least to a degree sufficient to understand the answers that you are given at Stackoverflow. - huffman
- oneThen why does this stackoverflow clone exist? Can you still advise to learn a linguist first? I understand English enough to understand the code, but in this matter there is too much theory. PS as always in domestic forums are ready to crap anyone and do anything except help directly. swagger Russian-speaking community rolls over. - mechanik0s
- The problem is in the amount of information and complexity. understanding the general tone of the sentence does not give an understanding of the important details. That's why I asked for help here. but as always, instead of words on a matter, everyone considers it their duty to pass by indicating how best to live or answer a question with a question. In the end, the topic is not so primitive and quite interesting so that the answer to it will help those who cannot ask a question in English. - mechanik0s
- 2> Then what is this stackoverflow clone for? @mechanikos agree, it still clearly does not exist then to translate the answers from stackoverflow. This is me to the fact that, besides references, it would be more reasonable to lay out the contents of the question itself - DreamChild
- one@mechanikos, if you want to get good answers, then take the trouble to ask good questions ( clearly formulated in Russian and, if possible, without reference to other pages). - avp
1 answer
@mechanikos look. Your code is as follows (I will bring it here for clarity):
int i = 255; byte b = (byte) i; int c; System.out.println(Integer.toBinaryString( i)); System.out.println("b = " + b); // b = -1 c=b>>>1; System.out.println(Integer.toBinaryString( c)); System.out.println(c); The fact is that byte is a sign type with a range of -128..127. this may seem somewhat unexpected, since it is logical to expect that a byte contains values ​​from 0 to 255 (say, in C # this is exactly so) However, it does not matter. It is important that casting int from a value of 255 to byte you will not get the expected value of 255 in variable b , but you will get -1, because in the additional code -1 in a single-byte variable will be 8 units. Judging by your code, you notice it. So then you apply to this byte an unsigned right shift, which is essentially a “real” shift, unlike >>, since it fills the left bits with zeros. As a result, by shifting the number to the right by one bit, you get the low-order bit equal to zero, which is why the number is no longer negative, since its left bit is reset.
But if you performed a shift in relation to c, then the result would be expected, since there would be no overflow in the cast (as well as the cast itself):
System.out.println(i >>> 1); - thanks for the answer. But there most of all problems because of coercion of types. experimentally and by rape of the brain, Jvadoki seemed to understand. how do I figure it out, try on a habr chtol throw an article, I think the topic is useful and will not get lost as posts in different blogs - mechanik0s
- 2> I think the topic is useful. Well, this is just the storage of negative numbers in the additional code. In fact, the topic is not as interesting as you think, since the algorithm of these transformations fits in literally two short steps. Read for example [here] [1] [1]: ru.wikipedia.org/wiki/… - DreamChild
- Nevertheless, this rubbish gives me the second day the brain takes out. Can I be so stupid and all the rest have figured it out so easily? ((((((and it all started with the implementation of the shift register in java ... - mechanik0s
- in principle, if I understood correctly, in order for everything to work as I expected and generally to have the opportunity to work at intervals from 0 to 255 in the byte, then you just need to always use the 0xFF mask and there will be a swell - mechanik0s
- > for everything to work as I expected and generally to work at intervals from 0 to 255 in byte, you just need to always use the 0хFF mask I don’t work with Java, and frankly, I don’t know how they avoid problems with the absence of unsigned types ( but they are not there), but I think that if you need values ​​from 0 to 255, then it would be logical to use a data type that includes this range - DreamChild