Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Finally, the
last group of operators we're going to look at is bitwise operators.
Now, this really requires me to go beyond the
scope of this course, I have to talk about some computer science stuff, and honestly
I don't really want to bore you, because this stuff doesn't really have that much
practical usage in the real world, I'm not saying their useless,
they definitely have their own use, but they are more on the theoretical
side and it's not something that you will use on a daily basis. However,
if you're really enthusiastic, I'm going to explain what
bitwise operators are, and how they work, but feel free to skip this lecture if you just want
to focus on the practicality. So you know that we
humans use the decimal system to represent numbers. So 1
2345. But in computers, these numbers are stored
in the binary format, which is a combination of 0's
and 1's. So let's take a look at a few examples. The
number 1 in the decimal system, is represented as 7
0's, 1234567, and 1.
So we have 8 digits, each digit is what we call
a bit, so here we have 8 bits, which represents
1 byte of information in a computer. Here's another
example. Number 2 in the decimal system equals to
there are six 0's and then 1, 0.
There are mathematical formulas behind this, for converting numbers between decimal
and binary systems, if you don't want to learn the mathematics, you can simply
Google decimal to binary converter. Now,
bitwise operators in JavaScript or any other programming languages, are
similar to logical operators, but they work in the individual
bits of a number. Let me show you what I mean by this.
So, I'm going to do a console.log, 1
or 2. Note that here we have a single
vertical line, that represents a bit wise or,
bitwise OR, logical or's
have double vertical lines. So when we apply the bitwise or
between 1 and 2, this is what's going to happen. So,
I add r as the result here. Now,
this operators is going to look at each of these beats like this,
in a vertical way, if either of these bits is one, the result
will be 1, otherwise it will be 0. So here, in both
these numbers, the first 6 bits are 0, so,
in the result you also have 6 0's. Now
the 7 bit will be 1, because here we have
1 at this position, right? And similarly, the 8 bit
will be 1, because here we have a 1. Now if we
convert this binary number to a decimal number the result will be
3. So let's save the changes
and you can see on the console we have 3.
Bitwise and a similar. So let's duplicate this, bitwise, and
will use a single ampersand for that.
ing 0, now the result is going to be something like this. So, if
both numbers are 1, 1 will be returned otherwise
will be 0. So in this case all the bits will end up
being 0, because if you look if you look at these two first numbers, 1 and 2,
their first 6 bits are 0,
and here in the second number we have this bit that is 1,
but the bit at the equivalent position here is 0. So if we apply
it with and between these two bits the results will be 0, okay?
So, this binary number equals 0 in the
decimal system. But save the changes, and see this on the console.
Save, here it is. So this is the fundamental
of bitwise operators. Now in JavaScript we have a few more bitwise operators,
but in the real world they are not that common. Now let me give you
a real world example of when we can use these bitwise operators so
at least they make sense to you. Imagine we want to implement
an access control system. So the user can have
these permissions, read, write or
execute. Now we can use one
byte of information, or 8 bits, to determine the kind of information
a user can have. For example, we can use
5 0's 12345, we don't care about
the first 5 bits, but for the other 3 bits,
if the user has a permission we will use 1, otherwise we will
use 0. So if the user has only the read permission, you will use
1 for the read, and for write and execute you will
use 0. Similarly if the user has read and
write permissions. We can represent this permission
using this binary number. And finally if the user
has all these positions. We can represent that
like this. Now, this is where we use bitwise operators.
So, I'm going to delete these two lines, as well as
these few lines on the top, let's see how we can implement
these access control system, using bitwise operators. So,
I'm going to define a constant called readPermission,
now I need to set this to a decimal number that is
equivalent to this binary number. That decimal number is
4. Again, you can use an online converter to get this. Similarly
I'm going to define another constant, writePermission
now what I want now is the decimal representation
of this binary number. So all these bits
or flags are off, only the right flag is
on it, okay? So this binary number 10, is equivalent to
2 in the decimal system. And finally,
executePermission. So what we want is the
decimal representation of this binary number. All these flags
are 0, oney the execute flag or execute bit is
1. And that number is 1.
Now, I can declare a variable like myPermission
inititally set that to 0, now I can give myself extra
permissions. So, I can update my permission like
this, myPermission, here we apply the bit
wise or to add a permission like read
Permission, as well as the write
permission. Now let's take a look at the
value of my permission on the console.
So, we get 6. Now technically, we don't care about this decimal
number, we can use the bitwise and operator to see
if I have a given permission. So here's an example,
I'm going to declare a variable, let's call that message,
now here I'm going to use the ternary operator, the conditional
operator, remember? So we start with a. condition, we take
myPermission, and use the bitwise and operator along
with readPermission. Now,
if this evalues to true, that means I have the readPermission.
So, let's say you want to display a message like
yes, otherwise we want to display no.
Let me break this up into multiple lines so you can see clearly. So here's our
ternary operator. You have a condition, if that evalues to true, this
value will be used, otherwise this value will be used. Finally,
let's log this message on the console. So I
have the readPermission. But if I move this readPermission here,
and save the changes. Now, you can see
our readPermission is gone. So here's what I want you to take away.
With the bitwise or operator, we can add
permissions, and with the bitwise and operator, we can
check to see if we have a given permission. Of course this is just 1
real world use case for using the bitwise operators, there are other use cases
but as I told you before, they are really not that common, so if this
lecture was confusing, don't worry about it.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.