Difference between revisions of "Bitwise Operations"

From Coder Merlin
m (Merlin moved page W1014 Boolean Algebra to W1014 Bitwise Operations without leaving a redirect: Correction)
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Bitwise Operations =
== Curriculum ==
 
{{MerlinCurriculumData|{{ROOTPAGENAME}}}}
== Background ==
== Bitwise Operations ==
A bitwise operation operates on one or more bit patterns at the level of their individual bits.  It is a fast, simple action supported by the ALU (Arithmetic Logic Unit) and is used to manipulate values for comparisons and calculations.  The normal operation is simply applied bit by bit.
A bitwise operation operates on one or more bit patterns at the level of their individual bits.  It is a fast, simple action supported by the ALU (Arithmetic Logic Unit) and is used to manipulate values for comparisons and calculations.  The normal operation is simply applied bit by bit.
 
=== NOT ===
<pre>
<pre>
NOT 0111
NOT 0111
Line 9: Line 9:
   = 1000
   = 1000
</pre>
</pre>
 
{{Caution|Note that because zero bits are "flipped" to one bits the actual size of the word matters.  For example,
<pre>
NOT 0111
  = 1000
</pre>
when considering only a four-bit word, but in the case of an eight-bit word the correct answer would be:
<pre>
  = 1111 1000
</pre>
As such, it's usually best to left-pad the operand with zeroes before proceeding.  For example,
<pre>
NOT 0000 0111
  = 1111 1000
</pre>
}}
=== AND ===
The AND operation is useful for masking bits that are not interesting and then checking to see if a particular bit is set.
The AND operation is useful for masking bits that are not interesting and then checking to see if a particular bit is set.
<pre>
<pre>
     0101
     0101
Line 18: Line 32:
   = 0001
   = 0001
</pre>
</pre>
 
=== OR ===
<pre>
<pre>
   0101
   0101
Line 25: Line 39:
  = 0111
  = 0111
</pre>
</pre>
 
=== XOR ===
The XOR operation is useful for inverting selected bits:
The XOR operation is useful for inverting selected bits:
<pre>
<pre>
     0101
     0101
Line 43: Line 56:
   = 0000
   = 0000
</pre>
</pre>
== Exercises ==
{{W1015-Exercises}}
{{Experience
|experienceID=W1015
|experienceUnit=Boolean algebra
|knowledgeAndSkills=§10.326
|topicAreas=Boolean algebra
|classroomTime=10 minutes
|studyTime=1 hour
|acquiredKnowledge=understand the use of bitwise operations with common Boolean operators
|acquiredSkill=demonstrate proficiency in using bitwise operations with common Boolean operators
}}

Revision as of 03:04, 22 June 2021

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Curriculum[edit]

ExercisesIcon.png
 Coder Merlin™  Computer Science Curriculum Data

Unit: Boolean algebra

Experience Name: Bitwise Operations (W1015)

Next Experience: ()

Knowledge and skills:

  • §10.326 Demonstrate understanding and proficiency in the use of bitwise operations

Topic areas: Boolean algebra

Classroom time (average): 10 minutes

Study time (average): 60 minutes

Successful completion requires knowledge: understand the use of bitwise operations with common Boolean operators

Successful completion requires skills: demonstrate proficiency in using bitwise operations with common Boolean operators

Bitwise Operations[edit]

A bitwise operation operates on one or more bit patterns at the level of their individual bits. It is a fast, simple action supported by the ALU (Arithmetic Logic Unit) and is used to manipulate values for comparisons and calculations. The normal operation is simply applied bit by bit.

NOT[edit]

NOT 0111
————————
  = 1000
CautionWarnIcon.png
Note that because zero bits are "flipped" to one bits the actual size of the word matters. For example,
NOT 0111
  = 1000

when considering only a four-bit word, but in the case of an eight-bit word the correct answer would be:

  = 1111 1000

As such, it's usually best to left-pad the operand with zeroes before proceeding. For example,

NOT 0000 0111
  = 1111 1000

AND[edit]

The AND operation is useful for masking bits that are not interesting and then checking to see if a particular bit is set.

    0101
AND 0011
————————
  = 0001

OR[edit]

   0101
OR 0011
————————
 = 0111

XOR[edit]

The XOR operation is useful for inverting selected bits:

    0101
XOR 0011
————————
  = 0110

Because of this property, XOR is sometimes used to clear a register (to zero) by XOR’ing it with itself. In some cases this can be much faster than explicitly loading the value of 0.

    1010
XOR 1010
————————
  = 0000

Exercises[edit]

Template:W1015-Exercises


Experience Metadata

Experience ID W1015
Next experience ID
Unit Boolean algebra
Knowledge and skills §10.326
Topic areas Boolean algebra
Classroom time 10 minutes
Study time 1 hour60 minutes <br />
Acquired knowledge understand the use of bitwise operations with common Boolean operators
Acquired skill demonstrate proficiency in using bitwise operations with common Boolean operators
Additional categories