- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
The Right Shift in Java
The Right Shift Make QR Code In Java Using Barcode generator for Java Control to generate, create Quick Response Code image in Java applications. QR Code ISO/IEC18004 Reader In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times Its general form is shown here: value >> num Barcode Creator In Java Using Barcode creation for Java Control to generate, create bar code image in Java applications. Barcode Reader In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. 4: Generating QR Code 2d Barcode In Visual C# Using Barcode drawer for .NET framework Control to generate, create QR-Code image in .NET framework applications. Encode QR Code JIS X 0510 In Visual Studio .NET Using Barcode creator for ASP.NET Control to generate, create QR Code image in ASP.NET applications. Operators
Making QR Code 2d Barcode In VS .NET Using Barcode creation for .NET framework Control to generate, create QR-Code image in Visual Studio .NET applications. Paint QR Code JIS X 0510 In Visual Basic .NET Using Barcode creation for .NET Control to generate, create QR Code 2d barcode image in .NET applications. Here, num specifies the number of positions to right-shift the value in value That is, the >> moves all of the bits in the specified value to the right the number of bit positions specified by num The following code fragment shifts the value 32 to the right by two positions, resulting in a being set to 8: Bar Code Drawer In Java Using Barcode creator for Java Control to generate, create barcode image in Java applications. GS1 RSS Generator In Java Using Barcode printer for Java Control to generate, create DataBar image in Java applications. int a = 32; a = a >> 2; // a now contains 8
Draw Linear Barcode In Java Using Barcode maker for Java Control to generate, create Linear 1D Barcode image in Java applications. Make ECC200 In Java Using Barcode generator for Java Control to generate, create Data Matrix ECC200 image in Java applications. When a value has bits that are shifted off, those bits are lost For example, the next code fragment shifts the value 35 to the right two positions, which causes the two low-order bits to be lost, resulting again in a being set to 8 Encoding Uniform Symbology Specification Code 93 In Java Using Barcode generator for Java Control to generate, create Code 93 Full ASCII image in Java applications. GS1 - 12 Recognizer In Visual Basic .NET Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications. int a = 35; a = a >> 2; // a still contains 8
Make EAN / UCC - 14 In None Using Barcode drawer for Online Control to generate, create EAN128 image in Online applications. Making UPC-A Supplement 2 In Objective-C Using Barcode maker for iPhone Control to generate, create Universal Product Code version A image in iPhone applications. Looking at the same operation in binary shows more clearly how this happens: 00100011 >> 2 00001000 35 8 Bar Code Drawer In Visual Studio .NET Using Barcode maker for ASP.NET Control to generate, create barcode image in ASP.NET applications. Create EAN13 In None Using Barcode printer for Excel Control to generate, create GTIN - 13 image in Excel applications. Each time you shift a value to the right, it divides that value by two and discards any remainder You can take advantage of this for high-performance integer division by 2 Of course, you must be sure that you are not shifting any bits off the right end When you are shifting right, the top (leftmost) bits exposed by the right shift are filled in with the previous contents of the top bit This is called sign extension and serves to preserve the sign of negative numbers when you shift them right For example, 8 >> 1 is 4, which, in binary, is 11111000 >>1 11111100 8 4 Reading Bar Code In VS .NET Using Barcode Control SDK for ASP.NET Control to generate, create, read, scan barcode image in ASP.NET applications. EAN13 Scanner In None Using Barcode decoder for Software Control to read, scan read, scan image in Software applications. It is interesting to note that if you shift 1 right, the result always remains 1, since sign extension keeps bringing in more ones in the high-order bits Sometimes it is not desirable to sign-extend values when you are shifting them to the right For example, the following program converts a byte value to its hexadecimal string representation Notice that the shifted value is masked by ANDing it with 0x0f to discard any sign-extended bits so that the value can be used as an index into the array of hexadecimal characters // Masking sign extension class HexByte { static public void main(String args[]) { char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; Part I: The Java Language
byte b = (byte) 0xf1; Systemoutprintln("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]); } } Here is the output of this program: b = 0xf1
The Unsigned Right Shift
As you have just seen, the >> operator automatically fills the high-order bit with its previous contents each time a shift occurs This preserves the sign of the value However, sometimes this is undesirable For example, if you are shifting something that does not represent a numeric value, you may not want sign extension to take place This situation is common when you are working with pixel-based values and graphics In these cases, you will generally want to shift a zero into the high-order bit no matter what its initial value was This is known as an unsigned shift To accomplish this, you will use Java s unsigned, shift-right operator, >>>, which always shifts zeros into the high-order bit The following code fragment demonstrates the >>> Here, a is set to 1, which sets all 32 bits to 1 in binary This value is then shifted right 24 bits, filling the top 24 bits with zeros, ignoring normal sign extension This sets a to 255 int a = -1; a = a >>> 24; Here is the same operation in binary form to further illustrate what is happening: 11111111 11111111 11111111 11111111 >>>24 00000000 00000000 00000000 11111111 1 in binary as an int 255 in binary as an int The >>> operator is often not as useful as you might like, since it is only meaningful for 32- and 64-bit values Remember, smaller values are automatically promoted to int in expressions This means that sign-extension occurs and that the shift will take place on a 32-bit rather than on an 8- or 16-bit value That is, one might expect an unsigned right shift on a byte value to zero-fill beginning at bit 7 But this is not the case, since it is a 32-bit value that is actually being shifted The following program demonstrates this effect: // Unsigned shifting a byte value class ByteUShift { static public void main(String args[]) { char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; byte b = (byte) 0xf1; byte c = (byte) (b >> 4); byte d = (byte) (b >>> 4); byte e = (byte) ((b & 0xff) >> 4); 4:
|
|