user69384: I have come accross a lot of examples using ways similar to the ones shown below to assign values to the pins.

PORTB |= (1<<PORTB2); //set bit 2


PORTB &= ~(1<<PORTB1); //clear bit 1


DDRD = DDRD | B11111100;  //Sets up the pins for output

My question is why do we need to use bitwise operations to make assignements?

user35469: If you want to change all pins on a port at once, you don't have to.
The point isn't just that you want to set/clear the pins you want, it's that you want to not change the state of the other pins.  To do that, you need to modify only the bits for the pins to be changed, and leave all the other bits as they are.  The only way to do this is with bitwise operators.
But if you do want to change all the pins at once, then you can perfectly well just set the port register to a value.  Outputting a value on an 8-bit bus would be a classic case where you'd do this.
I do need to pick you up on one thing in your question though.  In many cases, it is a bad idea to read the state of the port, modify the bits, and write the value back.  The reason is that almost all microcontrollers use the same port address for setting the output state and reading the input state.  So by reading back the port state, you aren't necessarily reading the actual outputs you set.  Logic-high outputs are generally weaker than logic-low, so it's entirely possible for your connected electronics to pull down a logic-high.  The sequence of faults for the bug goes:-

Your program sets a bit (pin) to 1.
Another component pulls that pin down to logic-low.
The next time you read that port back to change another pin, the input for the pin reads 0.
You modify the value for the pin you intended to change.
You write the value back.  Now not only has the intended pin changed state, but so has the pulled-down output.
The other component stops pulling the pin down.  But your micro keeps outputting 0 (logic-low) for that pin.

The solution is that you don't do bitwise operations directly on the register.  Instead, you keep a "shadow" variable containing the state you want the outputs to be.  You do all your bitwise operations on that shadow variable, and then you write the shadow variable directly to the register.  And the bug goes away.

user4143: 
My question is why do we need to use bitwise operations to make assignements?

Because you're not assigning, you're modifying.
If you want to turn on just pin 3 (for example) you need to only control bit 3 of PORTB and leave the others alone.
That means you need to "or" the value of just bit 3 being on with the existing PORTB to turn the bit on, or AND it with the inverse (all pins on except bit 3) to turn it off.
To get the value of bit 3 you can remember that it's 4, or you can just use 1<<3 to say "Bit 3" - or use the macro PORTB3 to make it more readable instead of just the number 3.
The operation is commonly known as "Read Modify Write" and in some low-end microcontrollers you have to take special care when doing it.
For example, if you want to control the output pin 3 and currently you have pins 2, 5 and 6 on and the rest of pins 0-7 off (those are the ones on port B on the Uno) the sequence may be like:
Turn on:

Calculate bit value 1<<3    => 0b00001000
Read PORTB                  => 0b01100100
OR the two values together  => 0b01101100
Write to PORTB              <= 0b01101100

Turn off:

Calculate bit value 1<<3    => 0b00001000
Invert it                   => 0b11110111
Read PORTB                  => 0b01101100
AND the two values together => 0b01100100
Write to PORTB              <= 0b01100100


69384: <p>I have come accross a lot of examples using ways similar to the ones shown below to assign values to the pins.</p>
<blockquote>
<p>PORTB |= (1&lt;&lt;PORTB2); //set bit 2</p>
</blockquote>
<blockquote>
<p>PORTB &amp;= ~(1&lt;&lt;PORTB1); //clear bit 1</p>
</blockquote>
<blockquote>
<p>DDRD = DDRD | B11111100;  //Sets up the pins for output</p>
</blockquote>
<p>My question is why do we need to use bitwise operations to make assignements?</p>
 35469: <h3>If you want to change all pins on a port at once, you don't have to.</h3>
<p>The point isn't just that you want to set/clear the pins you want, it's that you want to <em>not</em> change the state of the other pins.  To do that, you need to modify only the bits for the pins to be changed, and leave all the other bits as they are.  The only way to do this is with bitwise operators.</p>
<p>But if you do want to change all the pins at once, then you can perfectly well just set the port register to a value.  Outputting a value on an 8-bit bus would be a classic case where you'd do this.</p>
<p>I do need to pick you up on one thing in your question though.  In many cases, it is a bad idea to read the state of the port, modify the bits, and write the value back.  The reason is that almost all microcontrollers use the same port address for setting the output state and reading the input state.  So by reading back the port state, you aren't necessarily reading the actual outputs you set.  Logic-high outputs are generally weaker than logic-low, so it's entirely possible for your connected electronics to pull down a logic-high.  The sequence of faults for the bug goes:-</p>
<ul>
<li>Your program sets a bit (pin) to 1.</li>
<li>Another component pulls that pin down to logic-low.</li>
<li>The next time you read that port back to change another pin, the input for the pin reads 0.</li>
<li>You modify the value for the pin you intended to change.</li>
<li>You write the value back.  Now not only has the intended pin changed state, but so has the pulled-down output.</li>
<li>The other component stops pulling the pin down.  But your micro keeps outputting 0 (logic-low) for that pin.</li>
</ul>
<p>The solution is that you don't do bitwise operations directly on the register.  Instead, you keep a &quot;shadow&quot; variable containing the state you want the outputs to be.  You do all your bitwise operations on that shadow variable, and then you write the shadow variable directly to the register.  And the bug goes away.</p>
 4143: <blockquote>
<p>My question is why do we need to use bitwise operations to make assignements?</p>
</blockquote>
<p>Because you're not assigning, you're modifying.</p>
<p>If you want to turn on just pin 3 (for example) you need to only control bit 3 of PORTB and leave the others alone.</p>
<p>That means you need to &quot;or&quot; the value of just bit 3 being on with the existing PORTB to turn the bit on, or AND it with the inverse (all pins on except bit 3) to turn it off.</p>
<p>To get the value of bit 3 you can remember that it's <code>4</code>, or you can just use <code>1&lt;&lt;3</code> to say &quot;Bit 3&quot; - or use the macro <code>PORTB3</code> to make it more readable instead of just the number 3.</p>
<p>The operation is commonly known as &quot;Read Modify Write&quot; and in some low-end microcontrollers you have to take special care when doing it.</p>
<p>For example, if you want to control the output pin 3 and currently you have pins 2, 5 and 6 on and the rest of pins 0-7 off (those are the ones on port B on the Uno) the sequence may be like:</p>
<pre><code>Turn on:

Calculate bit value 1&lt;&lt;3    =&gt; 0b00001000
Read PORTB                  =&gt; 0b01100100
OR the two values together  =&gt; 0b01101100
Write to PORTB              &lt;= 0b01101100

Turn off:

Calculate bit value 1&lt;&lt;3    =&gt; 0b00001000
Invert it                   =&gt; 0b11110111
Read PORTB                  =&gt; 0b01101100
AND the two values together =&gt; 0b01100100
Write to PORTB              &lt;= 0b01100100
</code></pre>
