Forums

noob question :the |= operator !!

Started by essam November 17, 2011
i am using the Rowley crossWorks crossStudio for ARM with the board Philips LPC2148 (ARM) Prototype Board :
http://microcontrollershop.com/product_info.php?products_id83

i run a program that include the following lines of code :
IO0DIR |= (1 << LED_BIT); // set the pin for output
IO0CLR = (1 << LED_BIT); // set the output low

i wonder what "|=" means and why and how it is possible to use the stream insertion operator to output to a constant value "1".

please don't bother answering that,instead of giving me a fish teach me fishing.
i think i need to learn some ARM programming. do i need a book ??

BTW ,i have the book "Beginning C: From Novice to Professional " http://www.amazon.com/Beginning-Novice-Professional/dp/1590597354/ref=sr_1_1?ie=UTF8&qid21543056&sr=8-1

also : "ARM System Developer's Guide: Designing and Optimizing System Software" http://www.amazon.com/ARM-System-Developers-Guide-Architecture/dp/1558608745/ref=sr_1_1?s=books&ie=UTF8&qid21543143&sr=1-1
thank you very much in advance

An Engineer's Guide to the LPC2100 Series

> i wonder what "|=" means

> BTW ,i have the book "Beginning C: From Novice to Professional "

Go to page 70.

JW

Its nothing to do with stream operators !!! You need to read you C book .... focus on boolean and bitwise operators!

IO0DIR |= (1 << LED_BIT); // set the pin for output

This takes a value of 1, rotates it left LED_BIT times (whatever LED_BIT is defined as), then bitwise OR's it with the value in the IO0DIR register and writes the result back to the IO0DIR register !!

i.e. it just sets the bit LED_BIT to 1 in the IO0IR register.

It's no use having books unless you read them !!!

simon.

--- In l..., "essam" wrote:
>
> i am using the Rowley crossWorks crossStudio for ARM with the board Philips LPC2148 (ARM) Prototype Board :
> http://microcontrollershop.com/product_info.php?products_id83
>
> i run a program that include the following lines of code :
> IO0DIR |= (1 << LED_BIT); // set the pin for output
> IO0CLR = (1 << LED_BIT); // set the output low
>
> i wonder what "|=" means and why and how it is possible to use the stream insertion operator to output to a constant value "1".
>
> please don't bother answering that,instead of giving me a fish teach me fishing.
> i think i need to learn some ARM programming. do i need a book ??
>
> BTW ,i have the book "Beginning C: From Novice to Professional " http://www.amazon.com/Beginning-Novice-Professional/dp/1590597354/ref=sr_1_1?ie=UTF8&qid21543056&sr=8-1
>
> also : "ARM System Developer's Guide: Designing and Optimizing System Software" http://www.amazon.com/ARM-System-Developers-Guide-Architecture/dp/1558608745/ref=sr_1_1?s=books&ie=UTF8&qid21543143&sr=1-1
> thank you very much in advance
>

On 17/11/2011 15:22, essam wrote:
>
>
> i am using the Rowley crossWorks crossStudio for ARM with the board
> Philips LPC2148 (ARM) Prototype Board :
> http://microcontrollershop.com/product_info.php?products_id83
>
> i run a program that include the following lines of code :
> IO0DIR |= (1 << LED_BIT); // set the pin for output
> IO0CLR = (1 << LED_BIT); // set the output low
>
> i wonder what "|=" means and why and how it is possible to use the
> stream insertion operator to output to a constant value "1".
>
> please don't bother answering that,instead of giving me a fish teach me
> fishing.
> i think i need to learn some ARM programming. do i need a book ??
>
> BTW ,i have the book "Beginning C: From Novice to Professional "
> http://www.amazon.com/Beginning-Novice-Professional/dp/1590597354/ref=sr_1_1?ie=UTF8&qid21543056&sr=8-1
> also : "ARM System Developer's Guide: Designing and Optimizing System
> Software"
> http://www.amazon.com/ARM-System-Developers-Guide-Architecture/dp/1558608745/ref=sr_1_1?s=books&ie=UTF8&qid21543143&sr=1-1
>

This has nothing to do with it being an ARM, and nothing to do with
streams. It is basic C language. Yes you need a book, or at least to
read the one you already have. Here is a start though:

Look up |= on http://en.wikipedia.org/wiki/Augmented_assignment

and << on http://en.wikipedia.org/wiki/Bitwise_shift
Regards,
Richard.

+ http://www.FreeRTOS.org
Designed for Microcontrollers.
More than 7000 downloads per month.

the x+=y, x-=y, x|=y, x&=y are all C shortcut operators to perform the
same as x = x+y, x = x-y, etc

Get the "C Programming Language" to capture all these nuances. It's the
bible, and sits next to "C++ Programming Language" on my shelf

-parag
Parag B. Shah | Embedded Systems Engineer
3M Attenti | Track & Trace Solutions | SS&PS
1838 Gunn Hwy | Odessa, FL 33556
Office: 813.749.5454 x1337 | Fax: 813.749.5474
p...@3m.com | www.3M.com | www.3M.com/electronicmonitoring

From:
"essam"
To:
l...
Date:
11/17/2011 10:34 AM
Subject:
[lpc2000] noob question :the |= operator !!
Sent by:
l...

i am using the Rowley crossWorks crossStudio for ARM with the board
Philips LPC2148 (ARM) Prototype Board :
http://microcontrollershop.com/product_info.php?products_id83

i run a program that include the following lines of code :
IO0DIR |= (1 << LED_BIT); // set the pin for output
IO0CLR = (1 << LED_BIT); // set the output low

i wonder what "|=" means and why and how it is possible to use the stream
insertion operator to output to a constant value "1".

please don't bother answering that,instead of giving me a fish teach me
fishing.
i think i need to learn some ARM programming. do i need a book ??

BTW ,i have the book "Beginning C: From Novice to Professional "
http://www.amazon.com/Beginning-Novice-Professional/dp/1590597354/ref=sr_1_1?ie=UTF8&qid21543056&sr=8-1
also : "ARM System Developer's Guide: Designing and Optimizing System
Software"
http://www.amazon.com/ARM-System-Developers-Guide-Architecture/dp/1558608745/ref=sr_1_1?s=books&ie=UTF8&qid21543143&sr=1-1
thank you very much in advance



In case your not willing to read a book, read this page http://www.cplusplus.com/doc/tutorial/operators/
This website is targeted at C++, however a lot of it also applies to C.

--
Kevin

--- In l..., pbshah@... wrote:
>
> the x+=y, x-=y, x|=y, x&=y are all C shortcut operators to perform the
> same as x = x+y, x = x-y, etc
>
> Get the "C Programming Language" to capture all these nuances. It's the
> bible, and sits next to "C++ Programming Language" on my shelf
>
> -parag
> Parag B. Shah | Embedded Systems Engineer
> 3M Attenti | Track & Trace Solutions | SS&PS
> 1838 Gunn Hwy | Odessa, FL 33556
> Office: 813.749.5454 x1337 | Fax: 813.749.5474
> pbshah@... | www.3M.com | www.3M.com/electronicmonitoring
>
> From:
> "essam"
> To:
> l...
> Date:
> 11/17/2011 10:34 AM
> Subject:
> [lpc2000] noob question :the |= operator !!
> Sent by:
> l...
>
>
> i am using the Rowley crossWorks crossStudio for ARM with the board
> Philips LPC2148 (ARM) Prototype Board :
> http://microcontrollershop.com/product_info.php?products_id83
>
> i run a program that include the following lines of code :
> IO0DIR |= (1 << LED_BIT); // set the pin for output
> IO0CLR = (1 << LED_BIT); // set the output low
>
> i wonder what "|=" means and why and how it is possible to use the stream
> insertion operator to output to a constant value "1".
>
> please don't bother answering that,instead of giving me a fish teach me
> fishing.
> i think i need to learn some ARM programming. do i need a book ??
>
> BTW ,i have the book "Beginning C: From Novice to Professional "
> http://www.amazon.com/Beginning-Novice-Professional/dp/1590597354/ref=sr_1_1?ie=UTF8&qid21543056&sr=8-1
> also : "ARM System Developer's Guide: Designing and Optimizing System
> Software"
> http://www.amazon.com/ARM-System-Developers-Guide-Architecture/dp/1558608745/ref=sr_1_1?s=books&ie=UTF8&qid21543143&sr=1-1
> thank you very much in advance
>
>
>

Hi essam,

In fairness, for C programmers this might sound a stupid question, but in
the context of C++ the answer is slightly more obscure.

|= is simple shorthand

A |= B is the same as A = A | B.

The << operator is overloaded in C++, what it means depends on the context
in which it is used.

Using the built in types, if the left hand operand is a numeric type then it
performs a bitwise left shift and returns a numeric result.

If the left hand operator is a stream type, e.g. ofstream, then it
serializes the right hand operator to the stream indicated by the left
operator, and returns the stream as a result.

It is also possible for you to overload this for any of your own classes /
types to perform any operation you like.

So in C++,

A = cout << 1;

Is a valid line, it would serialize '1' to A and then assign the value of
cout to A ( provided a suitable assignment operator is defined also ).

A = 1 << 1;

is also valid, in this case since the left operand is numeric it assigns 2
to A.

So for C++ you need to be aware of the types of the operands in order to
fully understand the behaviour.

This is NOT related to ARM programming, it's a pure C / C++ question.

Regards

Phil.

From: l... [mailto:l...] On Behalf Of
p...@mmm.com
Sent: 17 November 2011 15:37
To: l...
Subject: Re: [lpc2000] noob question :the |= operator !!

the x+=y, x-=y, x|=y, x&=y are all C shortcut operators to perform the
same as x = x+y, x = x-y, etc

Get the "C Programming Language" to capture all these nuances. It's the
bible, and sits next to "C++ Programming Language" on my shelf

-parag

Parag B. Shah | Embedded Systems Engineer
3M Attenti | Track & Trace Solutions | SS&PS
1838 Gunn Hwy | Odessa, FL 33556
Office: 813.749.5454 x1337 | Fax: 813.749.5474
p...@3m.com | www.3M.com |
www.3M.com/electronicmonitoring

From:
"essam" >
To:
l...
Date:
11/17/2011 10:34 AM
Subject:
[lpc2000] noob question :the |= operator !!
Sent by:
l...

i am using the Rowley crossWorks crossStudio for ARM with the board
Philips LPC2148 (ARM) Prototype Board :
http://microcontrollershop.com/product_info.php?products_id83

i run a program that include the following lines of code :
IO0DIR |= (1 << LED_BIT); // set the pin for output
IO0CLR = (1 << LED_BIT); // set the output low

i wonder what "|=" means and why and how it is possible to use the stream
insertion operator to output to a constant value "1".

please don't bother answering that,instead of giving me a fish teach me
fishing.
i think i need to learn some ARM programming. do i need a book ??

BTW ,i have the book "Beginning C: From Novice to Professional "
http://www.amazon.com/Beginning-Novice-Professional/dp/1590597354/ref=sr_1_1
?ie=UTF8
1?ie=UTF8&qid21543056&sr=8-1> &qid21543056&sr=8-1

also : "ARM System Developer's Guide: Designing and Optimizing System
Software"
http://www.amazon.com/ARM-System-Developers-Guide-Architecture/dp/1558608745
/ref=sr_1_1?s=books
5/ref=sr_1_1?s=books&ie=UTF8&qid21543143&sr=1-1>
&ie=UTF8&qid21543143&sr=1-1

thank you very much in advance





> In fairness, for C programmers this might sound a stupid question, but
> in the context of C++ the answer is slightly more obscure.
>
> |= is simple shorthand
>
> A |= B is the same as A = A | B.

Not exactly--for this to be true, A must be side-effect-free.

> Using the built in types, if the left hand operand is a numeric type...

Read: "integer type"

--
Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk
SolderCore running Defender... http://www.vimeo.com/25709426

Look at the C operators.

DaveS

On Thu, Nov 17, 2011 at 7:22 AM, essam wrote:

> **
> i am using the Rowley crossWorks crossStudio for ARM with the board
> Philips LPC2148 (ARM) Prototype Board :
> http://microcontrollershop.com/product_info.php?products_id83
>
> i run a program that include the following lines of code :
> IO0DIR |= (1 << LED_BIT); // set the pin for output
> IO0CLR = (1 << LED_BIT); // set the output low
>
> i wonder what "|=" means and why and how it is possible to use the stream
> insertion operator to output to a constant value "1".
>
> please don't bother answering that,instead of giving me a fish teach me
> fishing.
> i think i need to learn some ARM programming. do i need a book ??
>
> BTW ,i have the book "Beginning C: From Novice to Professional "
> http://www.amazon.com/Beginning-Novice-Professional/dp/1590597354/ref=sr_1_1?ie=UTF8&qid21543056&sr=8-1
>
> also : "ARM System Developer's Guide: Designing and Optimizing System
> Software"
> http://www.amazon.com/ARM-System-Developers-Guide-Architecture/dp/1558608745/ref=sr_1_1?s=books&ie=UTF8&qid21543143&sr=1-1
>
> thank you very much in advance
>
>
>


--- In l..., "wekovci" wrote:
>
> > i wonder what "|=" means
>
> > BTW ,i have the book "Beginning C: From Novice to Professional "
>
> Go to page 70.
>
> JW
>

thank you .i actually have the kindle edition which does not use the notion of "pages" :)

i also would like to thank everybody :David Smead ,Paul Curtis, Phil Young, Kevin ,FreeRTOS Info , pbshah , simonb65 , wekovci.

i feel really ashamed . i knew a good bit of c++ so i assumed i know all about C .i was trying to figure out what is the matter with that line of code for the last 3 days , i had strong feeling that my question is a silly one but since i couldn't search for anything i gave up and asked the question here.

it is sad that i have to start with the C book before i put my hand in anything ARM. i thought i could start with some code samples first but i was mistaken.

brb starting with the C book.

cheers guys.