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

  • SEARCH
  • Home
  • 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 6914425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:20:45+00:00 2026-05-27T09:20:45+00:00

I am trying to combine three Byte values to one Long value, like System.Drawing.Color.ToArgb()

  • 0

I am trying to combine three Byte values to one Long value, like System.Drawing.Color.ToArgb() does.

I’ve looked up reference source code to find it and converted it to VB .NET:

Return CLng((CInt(red) << 16 Or CInt(green) << 8 Or blue Or CInt(alpha) << 24) And -1)

It works fine of course, but I don’t really understand why alpha shifted by 24 is last, not first (so it would be in nice order 24, 16, 8). I don’t really understand bitwise operations a lot. Also, this code snippet works for 4 bytes, which is redundant for me, I need just three bytes and I wonder if Long is still required when bytes are only three, wouldn’t Integer do just fine in this case?

So, my question is, how do I rewrite this code to work with just 3 parameters? Do I need to use Long? And, I know this is silly, but is it possible to make byte shifting in order 16,8,0 instead of 16,8,0,24? It’s really just aesthetics, but I am awful perfectionist.

Thanks in advance!

Aaron

  • 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-27T09:20:45+00:00Added an answer on May 27, 2026 at 9:20 am

    Here are some different ways. The order of the OR’s does not matter. I added some debugs so you can see the results as you go.

    Private Sub Button1_Click(sender As System.Object, _
                              e As System.EventArgs) Handles Button1.Click
    
        Dim bar As Integer '32 bits
    
        'three byte values
        Dim x As Byte = 66
        Dim y As Byte = 65
        Dim z As Byte = 82
    
        'desired byte order
        '0xyz
    
        'one byte at a time
        bar = 0
        bar = bar Or (CInt(x) << 16)
        Debug.WriteLine(Convert.ToString(bar, 2).PadLeft(32, "0"c))
        bar = bar Or (CInt(y) << 8)
        Debug.WriteLine(Convert.ToString(bar, 2).PadLeft(32, "0"c))
        bar = bar Or (CInt(z) << 0) 'bar Or CInt(z)
        Debug.WriteLine(Convert.ToString(bar, 2).PadLeft(32, "0"c))
    
        'or as single statement
        bar = 0 Or (CInt(x) << 16) Or (CInt(y) << 8) Or CInt(z)
        Debug.WriteLine(Convert.ToString(bar, 2).PadLeft(32, "0"c))
    
        'in any order
        bar = 0 Or CInt(z) Or (CInt(y) << 8) Or (CInt(x) << 16)
        Debug.WriteLine(Convert.ToString(bar, 2).PadLeft(32, "0"c))
    
        'check results
        Dim foo() As Byte = BitConverter.GetBytes(bar) 'get the individual bytes
        Array.Reverse(foo) 'reverse
        Debug.WriteLine(System.Text.ASCIIEncoding.ASCII.GetChars(foo, 1, 3))
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to combine the following three media queries into one: @media only screen
I'm trying to combine multiple column values into 1 and then label them distinctly
I'm trying to combine three signal waveforms into a single, interleaved waveform. I need
I am trying to combine using a std::set and a class, like this: #include
I am trying to combine the following two functions into one portable function: void
I'm working on a project where I'm trying to combine three different tables. Table
im trying to combine two images into a single image. unfortunately this has to
I'm trying to combine both the result of a semantic and a dictation request
I'm trying to combine 4 input fields into a jQuery function so that it
I'm trying to combine Spring with Hibernate using Annotations and I'm getting the following

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.