- In this video, we are going to talk about
binary numbers, and operations on binary numbers,
because this is one of the unique characteristics of C,
and it's not often discussed
in other programming languages.
We are calling those "Bit-Level Calculations,"
because at their core, inside the computer
data is represented as binary digits
or "bits" for short.
Some of the bit-level operators are:
"& AND, | OR," and "^ XOR"
which is short for 'exclusive or.'
Those operations should be familiar to you
if you had some background
with Base 10 calculations
which are assumed to be part of your readiness
for this video.
In this particular course,
we are not going to focus too deep
on bit-level operations
because this is an essential C.
It's only brought in as an overview
of the capability, and you are welcome
to explore other courses
that read this in a deeper manner.
The second group of Bit-Level Operators are:
"~ one's complement (0â1, 1â0),
which means we're going to switch
all the zeros to ones, and all the ones to zero
in a given number.
And the other one is ">> right shift,"
and "<< left shift," on which we'll talk
a little bit at greater length shortly.
First, let's have a refresher
of the meaning of the 'Truth Table,'
and in particular case,
the "and" bit-level operator.
What is a truth table?
If you're not familiar with it by now,
a truth table simply tells you
what is the outcome
of applying the operator
to two values
that can be either "true" or "false"
depending on the circumstance.
For example,
if both values are "true,"
then the "and" operator is also "true."
In some ways, there are parallels
between the bit-level "and,"
and a product of two digits.
For example, if you multiply 1 x 1,
the result is 1.
If you multiply 1 x 0,
the result is 0.
The same goes for bit-level "and."
"True and False" yield "False,
only "True and True" yield "True."
In a similar manner,
we can look at the Truth Table for "or."
This means, what would be the outcome
when two operands
are put together in an "or" expression?
You can, again, make analogies
between the addition operation,
although it's not the same as addition.
"True or True" yield "True."
"True or False" yield "True."
And so on, as you can see in the table.
Finally,
there is a more interesting operation
called 'exclusive or.'
In which,
when both values are the same,
either "True" or both "False,"
the result is "False."
Exclusive or yields true
only if the two values are different.
Let's put this to action in code.
For that purpose,
we don't have to do anything special
other than defining certain variables.
In this case,
"int" is 1 0,
and "y" is 1, and let's see
how we use them, and what'll be the result.
Line 30 depicts the usage
of a bit-level "and."
It takes the binary representation
of "x" and "y" from a bit-level operation
and displays the results as "int."
And so do the other following ones.
If you look at line 36,
this is the symbol for the "xor" operator.
Let's look at the outcome of those
before we continue with the right and left shift.
We'll run the program,
the first three represent
the outcomes of "and," "or,"
and logical "xor."
If you look at the values,
being 1 0 and 1,
you can easily see the outcome
of those operations if you know the rules
of "and," "or," and "xor."
It is more interesting to look at
the right shift and the left shift.
If you recall, from your knowledge of basic algebra,
in Base 10,
if you add a 0 at the end of a number
it's the same as multiplying that number by 10.
For example, if you have the number 12,
and you add a 0, it will be 120.
If you remove the 0,
it's as if you divided the number by 10.
Well, in binary arithmetic,
where we only have 0's and 1's,
the equivalent of multiplying by 10
or dividing by 10,
in a Base 2, is multiplying or dividing by two.
Therefore,
when you right shift a variable,
by one bit,
it's the same thing as dividing by two.
This is the same as saying in Base 10
you have 120,
you shift it to the right,
you drop the 0, and now you just have 12.
Which would be the same as dividing by 10.
Conversely,
when you left shift,
you take the value of "y,"
shift it one bit to the left,
it's the same as multiplying by two.
In Base 10 you would say,
"I have the number 12,
"I'm going to shift it to the left one position,
"which will be then filled by a zero.
"If I add a 0 at the end of 12,
"it's as if I multiply it by 10.
"So now it's 120."
In Base 2,
this is multiplying or dividing by two,
and then the result is converted
back into decimal numbers.
Again, the purpose of this video
is obviously not to teach everything there is to know
about binary operators, but just to give you
an overview of what C can do in the area of
logical bit-level operators.
Should you need to do this further,
there will be other opportunity
a little bit later in this course,
but primarily other courses,
that go deeper into
processing data in binary using the C language.