1
\$\begingroup\$

Obviously is the XOR-operator is used in low-level programming-languages used a lot for setting a value back to all bits 0.

For example:

11010 ^ 11010 => 00000

Brought my teacher to figuring a out few exercises.

One is: "Set the bits of a number back to 0 by using just the bitwise AND (&) and the bitwise NOT (~) operators."

I've got now this solution:

let readline = require('readline-sync'); const limit = 100; // THAT THE PART WHICH IS IMPORTANT TO ME !! function setNumberToNull(num) { // First invert the bits of the number. // Then compare every bit with the not // inverted number using an bitwise and. return num & (~num); } // ######################################## function testSetNumberToNull(limit) { let i; for (i = 0; i < limit; i++) { let testNum = Math.floor(Math.random() * 10000); let tmp = setNumberToNull(testNum); if (tmp) { return { test : i, message : testNum + ' caused failure.' }; } } return { test : i, message : 'All tests passed.'}; } console.log(testSetNumberToNull(limit).message);

I works fine. Nevertheless I would appreciate your review.

\$\endgroup\$
4
  • \$\begingroup\$The Stack Snippet you provided errors: ReferenceError: require is not defined.\$\endgroup\$
    – zyabin101
    CommentedJun 26, 2016 at 10:06
  • \$\begingroup\$Aside: readline should also be const.\$\endgroup\$
    – gcampbell
    CommentedJun 26, 2016 at 11:11
  • \$\begingroup\$@zyabin101 This is nodejs; require is a built-in function. It's not going to work on browsers, unfortunately.\$\endgroup\$
    – SirPython
    CommentedJun 27, 2016 at 2:50
  • \$\begingroup\$@zyabin101 It's like SirPython already said: It's something which works only with node.js, respectively this npm-module : npmjs.com/package/readline-sync\$\endgroup\$
    – mewi
    CommentedJun 27, 2016 at 5:57

1 Answer 1

2
\$\begingroup\$

Well, yes, num & (~num) does result in zero. Not much to review there. Seems you've solved the task you were given.

Of course, one might as well return/assign a literal zero, and skip two bitwise operations.

There's one JavaScript-specific caveat to all of this though: JavaScript's bitwise operations only work on 32-bit integers. However, that's not a datatype that usually exists in JavaScript: Numbers in JavaScript are all IEEE 756 64-bit floats.

The moment you use a bitwise operator on them, however, they are converted (with potential loss) to 32-bit ints, and the result then represented as a float again.

Of course, that's all academic, since whatever int representation you get, a & (~a) will result in zero.

But other bitwise operations can give strange results. For instance, OR'ing with zero should just return the number unaltered, but if the number's too large, its most-significant bit will end up being interpreted as the sign bit as the internal 32-bit conversion is always signed. So you get things like this:

2147483647 | 0 // => 2147483647 2147483648 | 0 // => -2147483648 

The second number is large enough to flip the high bit, and thus it's interpreted as negative.

Anyway, point is: Yes, a & (~a) will result in zero, for any value of a, despite JS's internal strangeness, because whatever it converts the input to, it also negates. But it's a special case; in other contexts bitwise operations in JS may be treacherous.

\$\endgroup\$
2
  • \$\begingroup\$Teacher mentioned that one can use the bitwise OR to get the integer-part of a number without the after decimal digits : "numberValue = numberValue | 0 " But I didn't know the background with the conversion. Awesome. Thanks a lot.\$\endgroup\$
    – mewi
    CommentedJun 27, 2016 at 6:12
  • \$\begingroup\$@mewi Yeah, the | 0 trick is the closest thing you have to a casting a number to an int in JS. The resulting number is still stored as a float, so it's more like (float) ((int) y) but the effect is essentially that decimals are discarded. Provided, of course, that the input fits in 32 bits. Which reminds me: I made a mistake in the answer. The MAX_SAFE_INTEGER constant tells you the largest int you can store in a float without precision loss, but that's a waaaay bigger number than you can store in a 32 bit int. I'll correct my answer\$\endgroup\$
    – Flambino
    CommentedJun 27, 2016 at 9:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.