472,780 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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 2350
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Glen Able | last post by:
Just to get my head straight on this... Firstly, am I right in thinking that right-shifting a signed integer has an undefined result (i.e. could be implemented as a logical or arithmetic shift)?...
80
by: Christopher Benson-Manica | last post by:
Of course one can get the effect with appropriate use of existing operators, but a ^^ operator would make for nice symmetry (as well as useful to me in something I'm working on). Am I the only one...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
4
by: Kevin | last post by:
I was looking through some source code and noticed the used of the C# << operator. Why is this being used here and under what circumstances is an left-shift operator useful. internal enum...
9
by: yuyang08 | last post by:
Dear all, Is there a logic shift operator in C++? I tried ">>", it is an arithmetic shift operator. Thanks! -Andy
24
by: Nishu | last post by:
Hi All, Could you please explain whether C standard supports logical right shift operation using some operator? I know somewhere I read about >>operator. I thought, it is in C but i think i'm...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.