2

I was working with a base64 encoding script, but it is throwing a lot of warnings in JSLint.

Can someone tell me what's the meaning of these symbols in JavaScript?

>>,<<,|,&

Here's an example of code with those symbols:

if ((c > 127) && (c < 2048)) {
    utftext += String.fromCharCode((c >> 6) | 192);
    utftext += String.fromCharCode((c & 63) | 128);
}

I would like to rewrite this so that it gets validated by JSLint.

Kobi
  • 125,267
  • 41
  • 244
  • 277
Couto
  • 823
  • 6
  • 16

3 Answers3

5

Those symbols refer to certain bitwise operations.

>> Right shift
<< Left shift
|  Bitwise OR
&  Bitwise AND

Read up on the linked Wikipedia page for more information on what they do.

See here for why JSLint warns on these operations. It largely has to do with efficiency (i.e., JavaScript uses floating point numbers internally and it's inefficient to cast to integers and back with bitwise operators).

Right shift and left shift can be replaced with dividing by and multiplying by 2, respectively.

Platinum Azure
  • 41,456
  • 9
  • 100
  • 130
0

This gets asked all the time, but it's difficult for some to find the right word to search on.

Those are bitwise operators.

zzzzBov
  • 157,699
  • 47
  • 307
  • 349
0

JSLint validates it fine for me when I declare the c and utftext variables. I don't see it complaining about any of these operators.

Pekka
  • 418,526
  • 129
  • 929
  • 1,058