So actually i know the solution of this problem, but i cant understand how its working. The following function get whole part of decimal value. Who can explain me, how its works?
function getDecimal(num) {
num = num << 1;
num = num >> 1;
return num;
}
console.log(getDecimal(123));
This function doesn't actually work for what you're trying to do. These examples will break it:
getDecimal(123.3) // returns 122
getDecimal(123) // returns 122
The way bitwise operators work in javascript is that they convert the number to an integer, do some processing, then convert it back to a javascript number. Effectively, your function does the following:
This is not the correct way of getting the integer part of a floating point number. Use Math.ceil(num)
instead.