Shift and Logical Operator - for what?

G

Guest

Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming?

I am not sure why i need to use it? I had some samples of c# codes using it.

Can someone share their experiences why someone should use those operators and what type of scenarios?

Any good snippets and example?

Thanks.
 
G

Guest

You can use the XOR operator for encryption (xoring a byte by a value changes the byte, xoring it back by that value changes it back to the original byte). This type of encryption is weak though.
 
S

Stefan Züger

For writing avarage applications these operations are most useful for
packing multiple informations in one variable. Examples are base64
encoding/decoding (see
http://www.thecodeproject.com/csharp/Base64EncDec.asp) or split a TCP/IP
address in network and node address parts. Most the of the hardware devices
exposes the registers with packed information to the software. I.e. in
device drivers you can find lots of logical and shift operations. Logical
and shift operations are also key for encryption/decryption algorithms.

Stefan


Chua Wen Ching said:
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR
XOR operator in our daily programming?
 
N

Nick Malik

Shift operators are used in compression/decompression routines, as well as
some math libraries.
XOR, OR, and AND operations are used frequently when driving hardware
libraries, and quite frequently for graphics systems.

These are all more commonly used in memory-constrained systems, which is not
as common as it used to be. That said, with the increased emphasis on
palm-top devices, there's still room for folks who care about using every
byte carefully.

--- Nick

Chua Wen Ching said:
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR
XOR operator in our daily programming?
 
J

John Wood

Shift operators are most often used in bit-arrays, such as flags. Generally
bit manipulation operators are useful as an efficient replacement for
2-to-the-power operations.
An example is something like:

if (myFlag & (1<<n)==1) ...

Which tests the n'th bit of myFlag. By the book you'd do this as:

if (myFlag & (Math.Pow(2, n+1))==1) ...

Which is far less efficient.

--
John Wood
email: john dot wood at priorganize dot com

Chua Wen Ching said:
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR
XOR operator in our daily programming?
 
C

cody

I had some beginner questions. Do we need Shift << >> or Logical AND OR
XOR operator in our daily programming?


In the KeyDown event the variable passed to the handler contains an enum
where certain bitflags are set for example wheather additionally to the key
Alt or Ctrl Key were down:

if ((key&Keys.Alt)!=0) { } // tests wheather Alt key were down
Key key = key &~Keys.Alt; // removes alt key from variable and returns pure
pressed key

But you're right, in C# bitmanipulations are used very rarely.
 
G

Guest

Thanks everyone for the tips.

Normally it is used for advance calculations. :)

Thanks again.
--
Regards,
Chua Wen Ching :)


Stefan Züger said:
For writing avarage applications these operations are most useful for
packing multiple informations in one variable. Examples are base64
encoding/decoding (see
http://www.thecodeproject.com/csharp/Base64EncDec.asp) or split a TCP/IP
address in network and node address parts. Most the of the hardware devices
exposes the registers with packed information to the software. I.e. in
device drivers you can find lots of logical and shift operations. Logical
and shift operations are also key for encryption/decryption algorithms.

Stefan


Chua Wen Ching said:
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR
XOR operator in our daily programming?
 
D

Duncan Mole

I have recently used bitwise & and | in hardware programming. for example
below:

public ASPILayer()

{

UInt32 retVal;

retVal = GetASPI32SupportInfo();


switch(((byte)((retVal & 0xFFFF) >> 8)))

{

case (byte)ErrorCode.SS_COMP:

hostAdaptCount = (byte)((retVal & 0xFFFF) & 0x00FF);

break;

case (byte)ErrorCode.SS_NO_ADAPTERS:

hostAdaptCount = 0;

break;

default:

throw new InitException();

}

}


ech0 said:
You can use the XOR operator for encryption (xoring a byte by a value
changes the byte, xoring it back by that value changes it back to the
original byte). This type of encryption is weak though.
 
M

Michael C

xor (^) is commonly used in encryption schemes, since xor'ing a value
against the same number twice gives you the original value. i.e., 255^1
gives you 254, and 254^1 gives you back the original 255. The shift
operators >> and << are also used to pack data and are used in many
encryption or base-conversion (decimal to hexadecimal numbers for instance)
schemes.

An old programmer's trick is to use << and >> to multiply or divide an
integer by a power of 2 (for instance (x >> 1) divides x by 2, and (x << 4)
multiplies x by 16). The shift operators are based on assembly language
instructions that generally operate much faster (fewer clock cycles) than
the equivalent integer multiply and divide instructions. So they can be
useful in other areas besides complex calculations.

Hope that helps,
Michael C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top