Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply bitwise operator to values of array

What would be the best way to apply the bitwise OR operator (or any operator I suppose) to an array of values in javascript?

var array = [1, 5, 18, 4];
// evaluate 1 | 5 | 18 | 4
like image 661
bflemi3 Avatar asked Dec 15 '22 10:12

bflemi3


1 Answers

Use reduce() and pass 0 as the initial value and logical or each value

var array = [1, 5, 18, 4];

var result = array.reduce(function(a, b) {
  return a | b;
}, 0);

console.log(result);
like image 199
AmmarCSE Avatar answered Dec 17 '22 01:12

AmmarCSE