Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

<< means shift left (moves the bits to the left side)

1 << 1 = 2 because (0001 << 1 = 0010 = 2)

3 << 1 = 6 because (0011 << 1 = 0110 = 6)

>> works in the same way

2 >> 1 = 1 because (0010 >> 1 = 0001)

The other operators are exactly the same you'd find in electronics (and boolean algebra):

~ is the NOT operator

& is the AND operator

^ is the XOR operator

Sorry, I don't have a visual representation for this (I haven't looked for a link) but I would strongly suggest to write it down on paper and do some small exercises (checking with the result then on your favorite programming language).

On a day to day basis, you'd likely not use these operators at all, but as soon as you start decoding protocols or working on a bit-level, those are super useful!

I would suggest trying to decode a bitstream protocol to really grasp the concepts: you'd use masks and bitwise operators a lot :)

---

Edit: some useful links I've found: https://www.interviewcake.com/concept/java/bit-shift



Right shift can get tricky for negative numbers. There are two operators for it, one which respects the sign bit, the other does not:

- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...


Testing…




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: