In lamens terms, what does a bitwise | operator do in Javascript and why is:
8 | 1 ; //9
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
8 in binary = 1000
1 in binary = 0001
if you take each binary digit and treat them as if statements (1 being true and 0 being false), you get this:
and the result is 1001, which is 9 in decimal
If it had been 8 & 1, it would go like this:
and the result would be 0
Here’s a quick example of how you might use these:
You would use the OR operator if you wanted to combine masks –
Using an example of file permissions, you may have the following flags:
If you wanted to create a mask for read and execute, you’d do something like
read value | execute valuewhich is1 | 4which would give you5(101 in bin)Now you have a mask that you can check a file’s permissions to see if it has both of these permissions, using the
&operator:Example file 1 (has read, write, and execute)
Its permission value is 7 (111 in bin): 111 & 101 = 101, so it does have those perms
Example file 2 (has read and write)
Its permission value is 6 (110 in bin): 110 & 101 = 100, so it only has the 100 (4 in dec) perms (read) of the two provided from the mask