336: Using the Right-Shift Operator in C#

Printer PDF417 in C# 336: Using the Right-Shift Operator
Listing 336: Using the Right-Shift Operator
PDF-417 2d Barcode Drawer In C#
Using Barcode printer for Visual Studio .NET Control to generate, create PDF417 image in .NET framework applications.
int x; x = (-7 >> 2); // 11111111111111111111111111111001 becomes // 11111111111111111111111111111110 // Write out "x is -2" SystemConsoleWriteLine("x = {0}", x);
Make Barcode In Visual C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create barcode image in .NET applications.
Output 317 shows the results of Listing 336
PDF417 Maker In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
OUTPUT 317:
Draw PDF-417 2d Barcode In Visual Studio .NET
Using Barcode generator for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
x = -2
Create PDF-417 2d Barcode In VB.NET
Using Barcode creator for Visual Studio .NET Control to generate, create PDF417 image in .NET applications.
Because of the right shift, the value of the bit in the rightmost location has dropped off the edge and the negative bit indicator on the left shifts by two locations to be replaced with 1s The result is -2
Print Barcode In C#
Using Barcode drawer for .NET Control to generate, create barcode image in Visual Studio .NET applications.
Bitwise Operators (&, |, ^) In some instances, you might need to perform logical operations, such as AND, OR, and XOR, on a bit-by-bit basis for two operands You do this via the &, |, and ^ operators, respectively
Creating Bar Code In Visual C#
Using Barcode generator for .NET Control to generate, create bar code image in VS .NET applications.
BEGINNER TOPIC Logical Operators Explained If you have two numbers, as shown in Figure 34, the bitwise operations will compare the values of the locations beginning at the leftmost significant value and continuing right until the end The value of 1 in a location is treated as true, and the value of 0 in a location is treated as false Therefore, the bitwise AND of the two values in Figure 34 would be the bit-by-bit comparison of bits in the first operand (12) with the bits in the second operand (7), resulting in the binary value 000000100, which is 4
Code39 Maker In Visual C#
Using Barcode creator for Visual Studio .NET Control to generate, create Code 39 Full ASCII image in .NET applications.
Bitwise Operators (<<, >>, |, &, ^, ~)
Encoding ANSI/AIM Code 128 In C#
Using Barcode creation for Visual Studio .NET Control to generate, create Code 128 image in .NET framework applications.
Figure 34: The Numbers 12 and 7 Represented in Binary
Draw DataMatrix In Visual C#
Using Barcode drawer for .NET framework Control to generate, create ECC200 image in VS .NET applications.
Alternatively, a bitwise OR of the two values would produce 00001111, the binary equivalent of 15 The XOR result would be 00001011, or decimal 11 Listing 337 demonstrates how to use these bitwise operators The results of Listing 337 appear in Output 318
Make Data Matrix 2d Barcode In Java
Using Barcode maker for Java Control to generate, create ECC200 image in Java applications.
Listing 337: Using Bitwise Operators
UPC-A Creation In .NET
Using Barcode generation for ASP.NET Control to generate, create UPC Code image in ASP.NET applications.
byte and, or, xor; and = 12 & 7; // and = 4 or = 12 | 7; // or = 15 xor = 12 ^ 7; // xor = 11 SystemConsoleWriteLine( "and = {0} \nor = {1}\nxor = {2}", and, or, xor);
Encoding ANSI/AIM Code 39 In .NET
Using Barcode printer for ASP.NET Control to generate, create Code39 image in ASP.NET applications.
OUTPUT 318:
DataMatrix Generation In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create Data Matrix 2d barcode image in ASP.NET applications.
and = 4 or = 15 xor = 11
Create Barcode In VB.NET
Using Barcode generator for VS .NET Control to generate, create barcode image in Visual Studio .NET applications.
In Listing 337, the value 7 is the mask; it is used to expose or eliminate specific bits within the first operand using the particular operator expression In order to convert a number to its binary representation, you need to iterate across each bit in a number Listing 338 is an example of a program that converts an integer to a string of its binary representation The results of Listing 338 appear in Output 319
Barcode Maker In .NET Framework
Using Barcode maker for ASP.NET Control to generate, create bar code image in ASP.NET applications.
Listing 338: Getting a String Representation of a Binary Display
Making Bar Code In VB.NET
Using Barcode generator for Visual Studio .NET Control to generate, create bar code image in .NET framework applications.
public class BinaryConverter { public static void Main()
Bar Code Creation In .NET
Using Barcode maker for ASP.NET Control to generate, create bar code image in ASP.NET applications.
3: Operators and Control Flow
Draw EAN-13 In Java
Using Barcode creation for Java Control to generate, create EAN-13 Supplement 5 image in Java applications.
{ const int size = 64; ulong value; char bit; SystemConsoleWrite ("Enter an integer: "); // Use longParse() so as to support negative numbers // Assumes unchecked assignment to ulong value = (ulong)longParse(SystemConsoleReadLine()); // Set initial mask to 100 ulong mask = 1ul << size - 1; for (int count = 0; count < size; count++) { bit = ((mask & value) > 0) '1': '0'; SystemConsoleWrite(bit); // Shift mask one location over to the right mask >>= 1; } SystemConsoleWriteLine(); } }
UPC-A Supplement 5 Decoder In VS .NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
OUTPUT 319:
Enter an integer: 42 0000000000000000000000000000000000000000000000000000000000101010
Notice that within each iteration of the for loop (discussed shortly), you use the right-shift assignment operator to create a mask corresponding to each bit in value By using the & bit operator to mask a particular bit, you can determine whether the bit is set If the mask returns a positive result, you set the corresponding bit to 1; otherwise, it is set to 0 In this way, you create a string representing the binary value of an unsigned long
Bitwise Assignment Operators (&=, |=, ^=) Not surprisingly, you can combine these bitwise operators with assignment operators as follows: &=, |=, and ^= As a result, you could take a variable, OR it with a number, and assign the result back to the original variable, which Listing 339 demonstrates