最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

typescript - Javascript bit masks - Stack Overflow

matteradmin4PV0评论

I've used bit masks in Java (not Javascript) before, but it's been a while and it's making me bug out.

This is the Typescript I'm wanting to work with. Theres 3 roles,

enum Type {
    FRUIT = 0x1,
    VEGGIE = 0x2,
    BOTH = FRUIT | VEGGIE
}

class Thing {
    role:number;

    constructor(role:Type){
        this.role = role;
    }
    getRole(){
        return this.role;
    }
    is(role:Type) {
        return !!(this.role & role);
    }
}

var whatever = new Thing(Type.FRUIT);

console.log('fruit', whatever.is(Type.FRUIT));
console.log('veggie', whatever.is(Type.VEGGIE));
console.log('both', whatever.is(Type.BOTH));

// fruit true
// veggie false
// both true

I conceptually see why "both" is ing back as "true", but my bit math isn't great.

When the role is either FRUIT or VEGGIE, the others are false. When it's set to BOTH, all should be true.

Tried a few binations of shifts and bit operations, but I can't get that output. If I try them separate it's fine, but I'd like to use binations of bits to build.

I've used bit masks in Java (not Javascript) before, but it's been a while and it's making me bug out.

This is the Typescript I'm wanting to work with. Theres 3 roles,

enum Type {
    FRUIT = 0x1,
    VEGGIE = 0x2,
    BOTH = FRUIT | VEGGIE
}

class Thing {
    role:number;

    constructor(role:Type){
        this.role = role;
    }
    getRole(){
        return this.role;
    }
    is(role:Type) {
        return !!(this.role & role);
    }
}

var whatever = new Thing(Type.FRUIT);

console.log('fruit', whatever.is(Type.FRUIT));
console.log('veggie', whatever.is(Type.VEGGIE));
console.log('both', whatever.is(Type.BOTH));

// fruit true
// veggie false
// both true

I conceptually see why "both" is ing back as "true", but my bit math isn't great.

When the role is either FRUIT or VEGGIE, the others are false. When it's set to BOTH, all should be true.

Tried a few binations of shifts and bit operations, but I can't get that output. If I try them separate it's fine, but I'd like to use binations of bits to build.

Share Improve this question edited Oct 2, 2015 at 10:16 pelumi 1,6701 gold badge13 silver badges21 bronze badges asked Sep 4, 2015 at 21:35 PhixPhix 9,9504 gold badges38 silver badges65 bronze badges 6
  • Just introduce a new class Type with isFruit and other flags. JS is not the language when you ought to use bitmasks and bit arithmetics. – zerkms Commented Sep 4, 2015 at 21:38
  • @zerkms why are you discouraging bitwise arithmetic in JavaScript? I've used it a number of times with great success--you just have to be aware that bitwise operations are limited to 32 bit integers in JS. – Dave Commented Sep 4, 2015 at 21:50
  • @dave any reason to prefer bit arithmetics over just boolean flags in JS? What is the technical reason to over-plicate the solution? – zerkms Commented Sep 4, 2015 at 22:11
  • What is your question exactly? The code looks fine to me – zhengbli Commented Sep 5, 2015 at 3:38
  • @zerkms if it's two or three static fags then boolean can be fine. If you're dealing with lots of flags thenot it is convenient to consolidate into one property. They also gives you flexibility to add a new flag without altering your data structure. – Dave Commented Sep 5, 2015 at 13:58
 |  Show 1 more ment

2 Answers 2

Reset to default 8

When you perform a bitwise & operation the result is that the bits turned on in both values used in the expression.

For example (taken from Pro TypeScript, p 207)

a 1011
&
b 1101
= 1001

The first column, both values are "switched on (1)", so the result is also switched on. The second column, a is switched off, so the result is 0, the third column b is off, so again a 0 and finally the last column both are on, so a 1.

In your case, it is even simpler because you are dealing with such small numbers...

a 01 // FRUIT
&
b 11 // ALL
= 01 // FRUIT

So the result is 01, or 1 if you have ten fingers.

If you use a double-bang !! to convert 1 to to a boolean (I call this slacker parsing), you'll get true because 1 is truth-y. This doesn't answer the question you were really asking, which is "does this bit flag match".

This is why this.role & role === role is the correct code, because you won't get a mistake from the "truthiness" of the value 1.

return !!(this.role & role);

Your version works as isAny, but you want it to work as isAll:

return (this.role & role) === role;
Post a comment

comment list (0)

  1. No comments so far