2014年5月26日星期一

Bitwise ] [ help on issues

 Bitwise   buffer   nio
looking java.nio packet's source code, which , Buffer class has recently such a method :

static void checkBounds (int off, int len, int size) {/ / package-private
if ((off | len | (off + len) | (size - (off + len))) <0)
throw new IndexOutOfBoundsException ();
}
It is used to check whether the array bounds , quilt class as the ByteBuffer get, put the method call :

public ByteBuffer get (byte [] dst, int offset, int length) {
checkBounds (offset, length, dst.length);
if (length> remaining ())
throw new BufferUnderflowException ();
int end = offset + length;
for (int i = offset; i dst [i] = get ();
return this;
}

My question is : checkBounds method that determines what is meant by the statement expression ( bit operation itself I have done to understand ) is off, len, (off + len) these expressions greater than 0 mean? Find doubts

------ Solution ------------------------------------ --------
mean that as long as there is a less than zero condition is established , enter judgment, an exception is thrown

only when several conditions are greater than 0 , the only condition is not satisfied , do not enter judgment not throw
------ Solution ------------- -------------------------------

      
If this is meant , then the "off + len" This expression is not redundant it?                
off + len subscript prevent overflow , is critical          
If off + len <0, then off, len at least one is negative , the conditions will be met in front of the (off <0 | | len <0), so I think this condition is redundant  
integer range is -2 ^ 31 to 2 ^ 31-1 , you feel such as the sum of two 2 ^ 31-1 , the result will be positive digital ?
		int off = Integer.MAX_VALUE, len = Integer.MAX_VALUE;
System.out.println(off);
System.out.println(len);
System.out.println(off + len);

output
2147483647
2147483647
-2
------ For reference only -------------------------------- -------
fact, and if (off <0 | | len <0 | | (off + len) < ; 0 | | (size - (off + len)) <0) similar results
The only difference is that logical or , just get a true stop immediately , does not check behind things, like if len <0, then checked off <0, len <0 immediately went to throw the exception , instead of continuing to check the condition of the back
The bit or is a calculation , you need to have or what the value of the four , only to judge whether < 0 , even if len <0, or will have completed all to judge
------ For reference only ---------------------------------------


If this is meant , then the "off + len" This expression is not redundant it?
------ For reference only -------------------------------------- -

  
If this is meant , then the "off + len" This expression is not redundant it?  
off + len is to prevent subscript overflow is critical
------ For reference only --------------------- ------------------

    
If this is meant , then the "off + len" This expression is not redundant it?          
off + len subscript prevent overflow , is critical  
If off + len <0, then off, len at least one is negative , the conditions will be met in front of the (off <0 | | len <0), so I think this condition is redundant
------ For reference only --------------------- ------------------

        
If this is meant , then the "off + len" This expression is not redundant it?                      
off + len subscript prevent overflow , is critical                
If off + len <0, then off, len at least one is negative , the conditions will be met in front of the (off <0 | | len <0), so I think this condition is redundant          
integer range is -2 ^ 31 to 2 ^ 31-1 , you feel such as the sum of two 2 ^ 31-1 , the result will be positive digital ?   
  
		int off = Integer.MAX_VALUE, len = Integer.MAX_VALUE;
System.out.println(off);
System.out.println(len);
System.out.println(off + len);
  
output   
2147483647   
2147483647   
-2  

shocked ! The original play this role ah
------ For reference only ------------------------------- --------
it seems , can not have a mathematical way of thinking , but also a thinking computer. Although the points have been given , but still would like to take this post down , this type int overflow Why will certainly become negative out?
------ For reference only -------------------------------------- -

this representation about int , int is 4 bytes from 0x00000000 ~ 0xFFFFFFFF, originally from 0 to 2 ^ 32-1 , but we must work to express a negative moment, most of the Language requirements from 0x00000000 ~ 0x7FFFFFFF is positive , ie, 0 to 2 ^ 31-1 , and from 0x80000000 to 0xFFFFFFFF start is negative , that is -2 ^ 31 to -1.

In an example above , Integer.MAX_VALUE is 0x7FFFFFFF,
0x7FFFFFFF +0 x7FFFFFFF = 0xFFFFFFFE
The 0xFFFFFFFE said -2
------ For reference only ----------------------------- ----------

this representation about int , int is 4 bytes from 0x00000000 ~ 0xFFFFFFFF, originally from 0 to 2 ^ 32-1 , but said negative going to work a little , most of the provisions from 0x00000000 ~ 0x7FFFFFFF language is positive , ie, 0 to 2 ^ 31-1 , and from 0x80000000 to 0xFFFFFFFF start is negative , that is -2 ^ 31 to -1.   
  
In an example above , Integer.MAX_VALUE is 0x7FFFFFFF,   
0x7FFFFFFF +0 x7FFFFFFF = 0xFFFFFFFE   
The 0xFFFFFFFE said -2  
learning , thank you for your patience Reply

------ For reference only ---------------------------------- -----
int type is 4 bytes , then you can represent 2 ^ 32 numbers, but no C + +, Java unsigned integer in them, so this is 2 ^ 32 digits necessary to represent negative numbers , but also represents a positive number , it took half of 2 ^ 32 is negative , half positive and 0 are non-negative , so the number of positive numbers is 2 ^ 31-1 , a negative number is 2 ^ 31 , so add up to exactly 2 ^ 32 th data type int .
highest bit is 0 and a positive , negative, the highest bit is 1, the result is a negative number after the spill , to look at the most significant bit is 0 after an overflow or 1 , it will not mean a positive spillover is negative
The following statement outputs would not be negative, but a positive number
System.out.println (Integer.MAX_VALUE * 5); results for 2147483643


------ For reference only ---------------------------------- -----

Well , int overflow necessarily negative, in the case of additive, multiplicative not necessarily

没有评论:

发表评论