gloopit gloopit - 1 year ago 40
Javascript Question

Bitwise operators, C vs JavaScript. Should I skip these exercises?

I'm studying reading and working on the exercises in the book, 'Programming in C' by Kochan.

I've just come across the 'Operations on Bits' section. I've read it and understand how the operations work at a basic, concept level.

I'm currently reading the book so get a better understanding of C as I'm studying CS50 and that's the language it uses to give an introduction to CS.

I know however that bitwise operations aren't actually used in the course.

My goal is to complete CS50 then move onto JavaScript and learn the full stack.

My question is whether you'd recommend getting diving deeper into the nitty gritty of bitwise operations in C and performing all the exercises (which may take me a whole day).

OR now that I have a general understanding of them, should I move on and just learn them in advanced depth, specific to JS when the time comes?

How much benefit would I get from doing it now or vice versa? How much will they translate from language to language? OR will it likely just confuse me?

Answer Source

Bitwise operators ~, &, | and ^ have very similar semantics between C, Java and javascript. Note however some differences:

  • in javascript, their operands can be floating point values, strings or other types. They are converted as 32 bit signed integers before applying the bitwise operator. This behavior can be used to force the conversion: x | 0 is the conversion of x as an int32. It is also used in asm.js as a hint that x should be compiled as an int ;
  • in C or Java, the operands must be integers and the rules for promotion and conversion of the operands are very specific.