PC Review


Reply
Thread Tools Rate Thread

<< and >> operators in c#

 
 
=?Utf-8?B?QmVu?=
Guest
Posts: n/a
 
      21st Jan 2007
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
 
Reply With Quote
 
 
 
 
=?UTF-8?B?QXJuZSBWYWpow7hq?=
Guest
Posts: n/a
 
      21st Jan 2007
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
 
Reply With Quote
 
Michael A. Covington
Guest
Posts: n/a
 
      22nd Jan 2007
> 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;



 
Reply With Quote
 
Steve Richter
Guest
Posts: n/a
 
      22nd Jan 2007

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

 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      22nd Jan 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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
>



 
Reply With Quote
 
Steve Richter
Guest
Posts: n/a
 
      22nd Jan 2007

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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > 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
> >


 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      22nd Jan 2007
> 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >
>> > 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
>> >

>



 
Reply With Quote
 
Steve Richter
Guest
Posts: n/a
 
      22nd Jan 2007

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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > 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" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> >
> >> > 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
> >> >

> >


 
Reply With Quote
 
Christof Nordiek
Guest
Posts: n/a
 
      22nd Jan 2007
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" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
>
> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >
>> > 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
>> >

>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Jan 2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
+ and & operators Domac Microsoft VB .NET 12 2nd Jul 2006 07:08 PM
static member operators vs non-member operators Ioannis Vranos Microsoft VC .NET 0 25th Oct 2004 05:58 PM
Operators =?Utf-8?B?SHk=?= Microsoft Access Queries 2 9th Mar 2004 02:25 PM
account operators reseting passwords for other account operators? Bob Gilbreath Microsoft Windows 2000 Active Directory 3 4th Feb 2004 09:32 AM
Account Operators can't manage other Account Operators account Mikael Oskarsson Microsoft Windows 2000 Active Directory 2 22nd Aug 2003 07:12 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:35 AM.