473,508 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

<< and >> operators in c#

Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?

Thanks,
Ben
Jan 21 '07 #1
9 2539
Ben wrote:
I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
I think you have seen too little code.

They are used - both for real bit manipulation
stuff and for multiplication and division with
powers of two.

Most CPU's actually implement them in their
native instruction set.
Arne
Jan 21 '07 #2
They are used - both for real bit manipulation
stuff and for multiplication and division with
powers of two.

Most CPU's actually implement them in their
native instruction set.
In fact that's why we have them.

k = j << 3;

is a lot faster than

k = j * 8;

Jan 22 '07 #3

Ben wrote:
Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?
it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve

Jan 22 '07 #4
Maybe you would like to expand on the 'piping and 'adding' bits.

Also, what makes you think that bit-shifting is seldom used?
"Steve Richter" <St************@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
>
Ben wrote:
>Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?

it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve

Jan 22 '07 #5

Stephany Young wrote:
Maybe you would like to expand on the 'piping and 'adding' bits.
arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.
Also, what makes you think that bit-shifting is seldom used?
I have not used bit shifting for years. If you are looking to multiply
or divide, better to use those operators. ( and the optimized compilers
of today likely bit shift when we multiply by a constant that is a
power of 2. )

-Steve

>

"Steve Richter" <St************@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...

Ben wrote:
Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?
it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve
Jan 22 '07 #6
arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.
And what language might that be defined in?
I have not used bit shifting for years. If you are looking to multiply
Just because you personally have not used a powerful element of the language
for years, don't assume that other people don't use it regularly.
"Steve Richter" <St************@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
>
Stephany Young wrote:
>Maybe you would like to expand on the 'piping and 'adding' bits.

arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.
>Also, what makes you think that bit-shifting is seldom used?

I have not used bit shifting for years. If you are looking to multiply
or divide, better to use those operators. ( and the optimized compilers
of today likely bit shift when we multiply by a constant that is a
power of 2. )

-Steve

>>

"Steve Richter" <St************@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegr oups.com...
>
Ben wrote:
Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?

it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve

Jan 22 '07 #7

Stephany Young wrote:
arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.

And what language might that be defined in?
a great language. one that also allows the programmer to define the
types that a class can be implicitly converted to. that is a feature
C# could benefit from. I dont like casting.
>
I have not used bit shifting for years. If you are looking to multiply

Just because you personally have not used a powerful element of the language
for years, don't assume that other people don't use it regularly.
what do they use it for? I think it makes a poor bit shift because it
ties you too close to the byte size of what is being operated on and
the unit of measure is implied as bits. why not a syntax that allows
you to shift by bits, bytes, characters?
wordValue = wordValue ShiftLeft 2 Bytes ;

-Steve
>

"Steve Richter" <St************@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...

Stephany Young wrote:
Maybe you would like to expand on the 'piping and 'adding' bits.
arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.
Also, what makes you think that bit-shifting is seldom used?
I have not used bit shifting for years. If you are looking to multiply
or divide, better to use those operators. ( and the optimized compilers
of today likely bit shift when we multiply by a constant that is a
power of 2. )

-Steve

>

"Steve Richter" <St************@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...

Ben wrote:
Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?

it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve
Jan 22 '07 #8
Hi Steve,

that would be using an operator to change one of it's operands. I think this
is not an OOP way to do it. It surely is not a C#-like way to do. (Though it
would be possible.)
One should use an Add-method instead of that (like in StringBuilder) or the
+ operator like in string, if the type has value semantic.

I suppose the inventors of C# felt this use of the << and >operators an
abuse of them, so they intentionally forbade it.
"Steve Richter" <St************@gmail.comschrieb im Newsbeitrag
news:11**********************@s34g2000cwa.googlegr oups.com...
>
Stephany Young wrote:
>Maybe you would like to expand on the 'piping and 'adding' bits.

arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.
>Also, what makes you think that bit-shifting is seldom used?

I have not used bit shifting for years. If you are looking to multiply
or divide, better to use those operators. ( and the optimized compilers
of today likely bit shift when we multiply by a constant that is a
power of 2. )

-Steve

>>

"Steve Richter" <St************@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegr oups.com...
>
Ben wrote:
Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?

it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve

Jan 22 '07 #9
Steve Richter wrote:
And what language might that be defined in?

a great language. one that also allows the programmer to define the
types that a class can be implicitly converted to. that is a feature
C# could benefit from. I dont like casting.
So you find << to be more obvious than list.Add(...) and you don't like
telling readers of your code when a conversion is taking place. I'm
beginning to spot a pattern here...

By the way, you *can* do implicit conversions in C#. There are some
restrictions, and personally I don't like them because they tend to
reduce readability, but they're available.

Jon

Jan 22 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
3283
by: seberino | last post by:
Tuples are defined with regards to parentheses ()'s as everyone knows. This causes confusion for 1 item tuples since (5) can be interpreted as a tuple OR as the number 5 in a mathematical...
2
3185
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
10537
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
1
1310
by: PengYu.UT | last post by:
I heard that != >= > <= operators are defined based on the operator == <. Could you tell me which header file defines the four operators? Best wishes, Peng
8
1552
by: Greenhorn | last post by:
Hi, Those relational operators have operands of all numerical type int,char,float etc. They also are working for character arrays. Whats the logic behind their working. Is the length of the...
1
2032
by: ±èÀçȲ | last post by:
//this code generates the error. uint a=1,b=2; Console.WriteLine(a << b); Console.WriteLine(a >> b); What problem does "uint type" have.?
5
2402
by: Suman | last post by:
Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++ archives and Stroustrup's FAQs (particularly the following: <url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>) I am...
3
3346
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
3
2929
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I...
2
1262
by: defn noob | last post by:
What does >and << do? Googling on them and they are just ignored...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.