Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 554057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:40:12+00:00 2026-05-13T11:40:12+00:00

I need to convert an array of integers to a little endian bitmask using

  • 0

I need to convert an array of integers to a little endian bitmask using Ruby. Any links or hints would be appreciated.

the example says [2,7,9,11] => “4205”

a = [2,7,9,11] # 4205
b = [1,2,3,4] # 0F00
def array_to_mask(arr)
  mask = 0
  arr.each do |i|
    mask = mask | (1 << i)
  end
  return mask.to_s(16)
end
p array_to_mask(a) # a84
p array_to_mask(b) # 1e

This does not work, but am i on the right track?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:40:12+00:00Added an answer on May 13, 2026 at 11:40 am

    Can’t you use arr.pack()? It has options for byte order.

    Update: Ok I’ve taken a look at the documentation you mentioned and the only way I can make the example work is this:

      7          2              11   9      (decimal index count)
    0 1 0 0  0 0 1 0  0 0 0 0  0 1 0 1      (bits)
    
       4        2        0        5         (nibbles, in hex)
    

    But that would mean that would mean that the 4205 are 4 nibbles that together represent 2 bytes? That is the only way I could make the first byte have the second and seventh bit set (reading little endian).

    … This is more of an ‘understanding the docs’ issue than it it a ruby issue.

    So the array solution is not the one you need, because you need to set the individual bits in a number. This is best achieved using (left) bit shift << and or’ing | the results together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem i need to convert from my Array structure to std::vector<int> ...
I need to convert a (possibly) null terminated array of ascii bytes to a
I need to convert CString to BYTE array. I don't know why, but everything
I need to convert a navigable map to a 2D String array. Below given
I have a string that I need to convert to the equivalent array of
I need to convert a HashMap<String, Object> to an array; could anyone show me
I got an array which contains signed int data, i need to convert each
How do I convert string or a value to a byte array? I need
Using LabVIEW 2009, I have a VI that outputs an array of U64 integers.
I am getting an array after some manipulation. I need to convert all array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.