Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  


AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Shift operators

java

+ Font mai mare | - Font mai mic



DOCUMENTE SIMILARE

Trimite pe Messenger
Windowing Components And Layout Classes
Choice, Menus, And Checkboxes
EJB3 Fundamentals
Applets And Graphics
Data Structures And Random Number Generation
Logical operators
Date And Advanced Classes
Toolkit, Window, Container, And Events
Auto increment and decrement
Seam Contexts


Shift operators

The shift operators also manipulate bits. They can be used solely with primitive, integral types. The left-shift operator (<<) produces the operand to the left of the operator shifted to the left by the number of bits specified after the operator (inserting zeroes at the lower-order bits). The signed right-shift operator (>>) produces the operand to the left of the operator shifted to the right by the number of bits specified after the operator. The signed right shift >> uses sign extension: if the value is positive, zeroes are inserted at the higher-order bits; if the value is negative, ones are inserted at the higher-order bits. Java has also added the unsigned right shift >>>, which uses zero extension: regardless of the sign, zeroes are inserted at the higher-order bits. This operator does not exist in C or C++.




If you shift a char, byte, or short, it will be promoted to int before the shift takes place, and the result will be an int. Only the five low-order bits of the right-hand side will be used. This prevents you from shifting more than the number of bits in an int. If you’re operating on a long, long will be the result. Only the six low-order bits of the right-hand side will be used so you can’t shift more than the number of bits in a long. There is a problem, however, with the unsigned right shift. If you use it with byte or short you might not get the correct results. (It’s broken in Java 1.0 and Java 1.1.) These are promoted to int and right shifted, but the zero extension does not occur, so you get -1 in those cases. The following example can be used to test your implementation:

//: URShift.java

// Test of unsigned right shift

public class URShift

} ///:~

Shifts can be combined with the equal sign (<<= or >>= or >>>=). The lvalue is replaced by the lvalue shifted by the rvalue.

Here’s an example that demonstrates the use of all the operators involving bits:

//: BitManipulation.java

// Using the bitwise operators

import java.util.*;

public class BitManipulation

static void pBinInt(String s, int i)

static void pBinLong(String s, long l)

} ///:~

The two methods at the end, pBinInt( ) and pBinLong( ) take an int or a long, respectively, and print it out in binary format along with a descriptive string. You can ignore the implementation of these for now.

You’ll note the use of System.out.print( ) instead of System.out.println( ). The print( ) method does not put out a new line, so it allows you to output a line in pieces.



As well as demonstrating the effect of all the bitwise operators for int and long, this example also shows the minimum, maximum, +1 and -1 values for int and long so you can see what they look like. Note that the high bit represents the sign: 0 means positive and 1 means negative. The output for the int portion looks like this:

-1, int: -1, binary:

11111111111111111111111111111111
+1, int: 1, binary:

00000000000000000000000000000001

maxpos, int: 2147483647, binary:

01111111111111111111111111111111

maxneg, int: -2147483648, binary:

10000000000000000000000000000000

i, int: 59081716, binary:

00000011100001011000001111110100

~i, int: -59081717, binary:

11111100011110100111110000001011

-i, int: -59081716, binary:

11111100011110100111110000001100

j, int: 198850956, binary:

00001011110110100011100110001100

i & j, int: 58720644, binary:

00000011100000000000000110000100

i | j, int: 199212028, binary:

00001011110111111011101111111100

i ^ j, int: 140491384, binary:

00001000010111111011101001111000

i << 5, int: 1890614912, binary:

01110000101100000111111010000000

i >> 5, int: 1846303, binary:

00000000000111000010110000011111

(~i) >> 5, int: -1846304, binary:

11111111111000111101001111100000

i >>> 5, int: 1846303, binary:

00000000000111000010110000011111

(~i) >>> 5, int: 132371424, binary:

00000111111000111101001111100000

The binary representation of the numbers is referred to as signed two’s complement.






Politica de confidentialitate



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 417
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2022 . All rights reserved

Distribuie URL

Adauga cod HTML in site