>>> Java extended shift operator unsigned Right shift

Source: Internet
Author: User
Tags bitwise bitwise operators rand

The arithmetic object that the shift operator faces is also the binary "bit". You can use them individually to handle integer types (one of the main types). The left shift operator (<<) is able to move the operand on the left side of the operator to the right of the specified number of digits on the right-hand side of the operator (0 in low). The signed right shift operator (>>) assigns the Operation object to the left of the operator to the right of the specified number of digits to the right-hand operator. The "signed" right shift operator uses the symbol extension: If the value is positive, insert 0 at the high level, or 1 in the high position if the value is negative. Java also adds a "unsigned" right shift operator (>>>), which uses "0 extensions": Inserts 0 at high levels, either positive or negative. This operator is not in C or C + +.
If Char,byte or short are shifted, they are automatically converted to an int before the shift occurs. Only 5 lows to the right are used. This prevents us from moving an unrealistic number of digits in an int. If a long value is processed, the resulting result is long. Only the 6 lows on the right are used at this point to prevent moving more than the number of bits in the Long value. However, there may also be a problem with the "unsigned" right shift. If you shift the byte or short value to the right, you may not get the correct result (Java 1.0 and Java 1.1 are outstanding). They are automatically converted to type int and are shifted to the right. But "0 extensions" don't happen, so in those cases you get the result of 1. You can use the following example to detect your own implementation scenarios:
: Urshift.java
Test of unsigned Right shift
public class Urshift {
public static void Main (string[] args) {
int i =-1;
I >>>= 10;
System.out.println (i);
Long L =-1;
L >>>= 10;
System.out.println (l);
Short S =-1;
S >>>= 10;
System.out.println (s);
byte B =-1;
b >>>= 10;
System.out.println (b);
} ///:~
The shift can be used in combination with an equal sign (<<= or >>= or >>>=). At this point, the value on the left side of the operator moves the number of digits specified by the value on the right, and then assigns the resulting result back to the value on the left.
Here's an example of how to apply all the operators that involve a bitwise operation and their effects:
: Bitmanipulation.java
Using The bitwise operators
Import java.util.*;
public class Bitmanipulation {
public static void Main (string[] args) {
Random rand = new Random ();
int i = Rand.nextint ();
Int J = Rand.nextint ();
Pbinint ("-1",-1);
Pbinint ("+1", + 1);
int maxpos = 2147483647;
Pbinint ("Maxpos", Maxpos);
int Maxneg =-2147483648;
Pbinint ("Maxneg", Maxneg);
Pbinint ("I", I);
Pbinint ("~i", ~i);
Pbinint ("I",-i);
Pbinint ("J", J);
Pbinint ("I & J", I & J);
Pbinint ("I J", I j);
Pbinint ("I ^ j", I ^ j);
Pbinint ("I << 5", I << 5);
Pbinint ("I >> 5", I >> 5);
Pbinint ("(~i) >> 5", (~i) >> 5);
Pbinint ("I >>> 5", I >>> 5);
Pbinint ("(~i) >>> 5", (~i) >>> 5);
Long L = Rand.nextlong ();
Long m = Rand.nextlong ();
Pbinlong (" -1l", -1l);
Pbinlong ("+1l", +1l);
Long ll = 9223372036854775807L;
Pbinlong ("Maxpos", LL);
Long Lln = -9223372036854775808l;
Pbinlong ("Maxneg", Lln);
Pbinlong ("L", l);
Pbinlong ("~l", ~l);
Pbinlong ("L",-l);
Pbinlong ("M", m);
Pbinlong ("L & M", L & M);
Pbinlong ("L M", l m);
Pbinlong ("l ^ m", l ^ m);
Pbinlong ("L << 5", L << 5);
Pbinlong ("L >> 5", L >> 5);
Pbinlong ("(~l) >> 5", (~L) >> 5);
Pbinlong ("L >>> 5", L >>> 5);
Pbinlong ("(~l) >>> 5", (~L) >>> 5);

static void Pbinint (String s, int i) {
System.out.println (
S + ", int:" + i + ", binary:");
System.out.print ("");
for (int j = J >=0; j--)
if (((1 << j) & I)!= 0)
System.out.print ("1");
System.out.print ("0");
System.out.println ();
static void Pbinlong (String s, long L) {
System.out.println (
S + ", Long:" + L + ", Binary:");
System.out.print ("");
for (int i = i >=0; i--)
if (((1L << i) & L)!= 0)
System.out.print ("1");
System.out.print ("0");
System.out.println ();
} ///:~
Two methods were called at the end of the program: Pbinint () and Pbinlong (). They manipulate an int and a long value and output in a binary format with a brief explanatory text. At present, they can temporarily ignore their specific implementation scenarios.
You should focus on the use of System.out.print () rather than System.out.println (). The print () method does not produce a new row to list multiple information in the same row.
In addition to showing the effects of all bitwise operators on int and long, this example shows the minimum, maximum, +1, and -1 values of int and long, so that you can appreciate their situation. Pay attention to the high position of positive sign: 0 is positive, 1 is negative. The output of the INT section is listed below:
-1, int:-1, binary:
+1, int:1, binary:
Maxpos, int:2147483647, binary:
Maxneg, int: -2147483648, binary:
I, int:59081716, binary:
~i, int: -59081717, binary:
-I, int: -59081716, binary:
J, int:198850956, binary:
I & J, int:58720644, binary:
I j, int:199212028, binary:
I ^ j, int:140491384, binary:
I << 5, int:1890614912, binary:
I >> 5, int:1846303, binary:
(~i) >> 5, int: -1846304, binary:
I >>> 5, int:1846303, binary:
(~i) >>> 5, int:132371424, binary:
The binary form of a number is expressed as "a complement of signed 2".


Data reference: http://www.knowsky.com/371432.html

Related Article

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.