Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

76
Views
Optimice la conversión de/a objeto<->número con un giro de bits

Estoy buscando optimizar mi código que convierte un objeto en una representación numérica y viceversa. Actualmente estoy usando las funciones parseInt y toString, pero me gustaría hacer esto usando un poco de giro. ¿Tienes alguna idea de cómo puedo hacer esto?

 interface Item { flexType: number; // 1 bit flexNum: number; // 7 bits clearType: number; // 1 bit clearNum: number; // 7 bits weight: number; // 16 bits } const itemToNumber = (item: Item) => { const bitsFlexType = item.flexType.toString(2); const bitsFlexNum = item.flexNum.toString(2); const bitsClearType = item.clearType.toString(2); const bitsClearNum = item.clearNum.toString(2); const bitsWeight = item.weight.toString(2); return parseInt( [bitsFlexType, bitsFlexNum, bitsClearType, bitsClearNum, bitsWeight].join( "" ), 2 ); }; const numberToItem = (number: number) => { const bits = number.toString(2); const item: Item = { flexType: parseInt(bits[0], 2), flexNum: parseInt(bits.slice(1, 7), 2), clearType: parseInt(bits[7], 2), clearNum: parseInt(bits.slice(8, 15), 2), weight: parseInt(bits.slice(15), 2) }; return item; }; const numericItem = itemToNumber({ flexType: 1, flexNum: 42, clearType: 0, clearNum: 100, weight: 1002 }); const item = numberToItem(numericItem); console.log(item);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!