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 5837859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:24:23+00:00 2026-05-22T11:24:23+00:00

Is there a method (in c#/.net) that would left-shift (bitwise) each short in a

  • 0

Is there a method (in c#/.net) that would left-shift (bitwise) each short in a short[] that would be faster then doing it in a loop?

I am talking about data coming from a digital camera (16bit gray), the camera only uses the lower 12 bits. So to see something when rendering the data it needs to be shifted left by 4.

This is what I am doing so far:

byte[] RawData; // from camera along with the other info

if (pf == PixelFormats.Gray16)
{
    fixed (byte* ptr = RawData)
    {
        short* wptr = (short*)ptr;
        short temp;

        for (int line = 0; line < ImageHeight; line++)
        {
            for (int pix = 0; pix < ImageWidth; pix++)
            {
                temp = *(wptr + (pix + line * ImageWidth));
                *(wptr + (pix + line * ImageWidth)) = (short)(temp << 4);
            }
        }
    }
}

Any ideas?

  • 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-22T11:24:23+00:00Added an answer on May 22, 2026 at 11:24 am

    I don’t know of a library method that will do it, but I have some suggestions that might help. This will only work if you know that the upper four bits of the pixel are definitely zero (rather than garbage). (If they are garbage, you’d have to add bitmasks to the below). Basically I would propose:

    • Using a shift operator on a larger data type (int or long) so that you are shifting more data at once
    • Getting rid of the multiply operations inside your loop
    • Doing a little loop unrolling

    Here is my code:

    using System.Diagnostics;
    
    namespace ConsoleApplication9 {
      class Program {
        public static void Main() {
          Crazy();
        }
    
        private static unsafe void Crazy() {
          short[] RawData={
            0x000, 0x111, 0x222, 0x333, 0x444, 0x555, 0x666, 0x777, 0x888,
            0x999, 0xaaa, 0xbbb, 0xccc, 0xddd, 0xeee, 0xfff, 0x123, 0x456,
    
            //extra sentinel value which is just here to demonstrate that the algorithm
            //doesn't go too far
            0xbad 
          };
    
          const int ImageHeight=2;
          const int ImageWidth=9;
    
          var numShorts=ImageHeight*ImageWidth;
    
          fixed(short* rawDataAsShortPtr=RawData) {
            var nextLong=(long*)rawDataAsShortPtr;
    
            //1 chunk of 4 longs
            // ==8 ints
            // ==16 shorts
            while(numShorts>=16) {
              *nextLong=*nextLong<<4;
              nextLong++;
              *nextLong=*nextLong<<4;
              nextLong++;
              *nextLong=*nextLong<<4;
              nextLong++;
              *nextLong=*nextLong<<4;
              nextLong++;
    
              numShorts-=16;
            }
    
            var nextShort=(short*)nextLong;
            while(numShorts>0) {
              *nextShort=(short)(*nextShort<<4);
              nextShort++;
              numShorts--;
            }
          }
    
          foreach(var item in RawData) {
            Debug.Print("{0:X4}", item);
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a .NET build-in method that would solve my scenario? Got an array
There is any command line or .NET method that runs a process in the
Is there one method or property in the .NET Framework that tells if an
Is there a class existing in .NET Framework 3.5 that would be equivalent to
There was an Html.RadioButtonList extension method in ASP.NET MVC Futures. Has anyone found a
What encryption method does the .NET FormsAuthentication.Encrypt() method use? There's no mention in the
Is there a way to Invoke an overloaded method using reflection in .NET (2.0).
I'm using ASP.NET Membership and noticed there isn't a method in the Roles class
The case: There is a .net application calling unmanaged C code. Used method for
Using ASP.Net MVC 2, is there any way to use the RedirectToAction() method of

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.