469,640 Members | 1,579 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,640 developers. It's quick & easy.

Shift and Logical Operator - for what?

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.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
8 2183
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.

"Chua Wen Ching" wrote:
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.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #2
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" <ch************@nospam.hotmail.com> wrote in message
news:1D**********************************@microsof t.com...
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.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #3
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" <ch************@nospam.hotmail.com> wrote in message
news:1D**********************************@microsof t.com...
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.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #4
> 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.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #5
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" <ch************@nospam.hotmail.com> wrote in message
news:1D**********************************@microsof t.com...
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.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #6
Thanks everyone for the tips.

Normally it is used for advance calculations. :)

Thanks again.
--
Regards,
Chua Wen Ching :)
"Stefan Z√ľger" wrote:
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" <ch************@nospam.hotmail.com> wrote in message
news:1D**********************************@microsof t.com...
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.
--
Regards,
Chua Wen Ching :)


Nov 16 '05 #7
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" <ec**@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
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.
"Chua Wen Ching" wrote:
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.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #8
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.
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:1D**********************************@microsof t.com...
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.
--
Regards,
Chua Wen Ching :)


Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Glen Able | last post: by
80 posts views Thread by Christopher Benson-Manica | last post: by
43 posts views Thread by Mehta Shailendrakumar | last post: by
4 posts views Thread by Kevin | last post: by
9 posts views Thread by yuyang08 | last post: by
24 posts views Thread by Nishu | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.