Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

#Bit field

Bit field

Just a quick note on the large and hacky switch statement you use to create selected options.

The variable art is a bit field, with the first bit for the top selection and to the third bit the bottom. The whole switch can be simplified with a bit of binary logic and math

art = art < 8 ? art : 0; // to make art does not contain values out of range.
$(["naturnah","stadtnah","innerstädtisch"].reduce((str, name, i) => {
 return `${str}<option 
 value="${(1 << i).toString(2).padStart(3,"0")}" 
 ${art & (1 << i) ? " selected" : ""}>${name}</option>`;
 },""
).appendTo("#route-update-routetype");
$update_route_type.trigger('contentChanged');

To check a bit you use bitwise & (and) to mask (remove) all unwanted bits. If the result is not zero the bit is on. To create the mask shift 1 left to the bit position you want.

const isBitOn = (bitField, bit) => !(bitField & (1 << bit));
// ^ ^ ^^ ^^^ bit number
// | | ||
// | | || shift left
// | | 
// | | bitwise & to mask out unwanted bits
// |
// | Not to check result is not zero

#Bit field

Just a quick note on the large and hacky switch statement you use to create selected options.

The variable art is a bit field, with the first bit for the top selection and to the third bit the bottom. The whole switch can be simplified with a bit of binary logic and math

art = art < 8 ? art : 0; // to make art does not contain values out of range.
$(["naturnah","stadtnah","innerstädtisch"].reduce((str, name, i) => {
 return `${str}<option 
 value="${(1 << i).toString(2).padStart(3,"0")}" 
 ${art & (1 << i) ? " selected" : ""}>${name}</option>`;
 },""
).appendTo("#route-update-routetype");
$update_route_type.trigger('contentChanged');

To check a bit you use bitwise & (and) to mask (remove) all unwanted bits. If the result is not zero the bit is on. To create the mask shift 1 left to the bit position you want.

const isBitOn = (bitField, bit) => !(bitField & (1 << bit));
// ^ ^ ^^ ^^^ bit number
// | | ||
// | | || shift left
// | | 
// | | bitwise & to mask out unwanted bits
// |
// | Not to check result is not zero

Bit field

Just a quick note on the large and hacky switch statement you use to create selected options.

The variable art is a bit field, with the first bit for the top selection and to the third bit the bottom. The whole switch can be simplified with a bit of binary logic and math

art = art < 8 ? art : 0; // to make art does not contain values out of range.
$(["naturnah","stadtnah","innerstädtisch"].reduce((str, name, i) => {
 return `${str}<option 
 value="${(1 << i).toString(2).padStart(3,"0")}" 
 ${art & (1 << i) ? " selected" : ""}>${name}</option>`;
 },""
).appendTo("#route-update-routetype");
$update_route_type.trigger('contentChanged');

To check a bit you use bitwise & (and) to mask (remove) all unwanted bits. If the result is not zero the bit is on. To create the mask shift 1 left to the bit position you want.

const isBitOn = (bitField, bit) => !(bitField & (1 << bit));
// ^ ^ ^^ ^^^ bit number
// | | ||
// | | || shift left
// | | 
// | | bitwise & to mask out unwanted bits
// |
// | Not to check result is not zero
Source Link
Blindman67
  • 22.8k
  • 2
  • 16
  • 40

#Bit field

Just a quick note on the large and hacky switch statement you use to create selected options.

The variable art is a bit field, with the first bit for the top selection and to the third bit the bottom. The whole switch can be simplified with a bit of binary logic and math

art = art < 8 ? art : 0; // to make art does not contain values out of range.
$(["naturnah","stadtnah","innerstädtisch"].reduce((str, name, i) => {
 return `${str}<option 
 value="${(1 << i).toString(2).padStart(3,"0")}" 
 ${art & (1 << i) ? " selected" : ""}>${name}</option>`;
 },""
).appendTo("#route-update-routetype");
$update_route_type.trigger('contentChanged');

To check a bit you use bitwise & (and) to mask (remove) all unwanted bits. If the result is not zero the bit is on. To create the mask shift 1 left to the bit position you want.

const isBitOn = (bitField, bit) => !(bitField & (1 << bit));
// ^ ^ ^^ ^^^ bit number
// | | ||
// | | || shift left
// | | 
// | | bitwise & to mask out unwanted bits
// |
// | Not to check result is not zero
default

AltStyle によって変換されたページ (->オリジナル) /