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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:40:45+00:00 2026-05-25T19:40:45+00:00

I need to take an 8 bit number on a 64 bit cpu and

  • 0

I need to take an 8 bit number on a 64 bit cpu and shift it to the right 8 times. Each time I shift the number I need to shift the same 8 bit number in behind it so that I end up with the same 8 bit number repeating 8 times. This would end up being shift, add 8, shift add 8… etc. which ends up being 40+ cycles (correct me if I’m wrong).

Is there a way to do this operation (of shift and copy) in 1 cycle so that I end up with the same value in the end?

long _value = 0;
byte _number = 7;
for (int i = 0; i < 8; i++) {
    _value = (_value << 8) + _number;
}

EDIT: I’m trying to compare a stream of chars to detect keywords. I can’t use string.contains because the string value may be across the boundary of the buffer. Also, the application has to run on embedded ARM cpus as well as desktop and server CPUs. Memory usage and cpu cycles are very important.

  • 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-25T19:40:45+00:00Added an answer on May 25, 2026 at 7:40 pm

    Another idea would be to precompute all for all values of byte a lookup table.

    var lu = new long[256];
    // init
    var n = 7;
    var v = lu[n];
    

    Update

    Some benchmark results (in ms per 100000000 iterations):

    • Loop: 272
    • Unrolled: 207
    • Unsafe: 351
    • Lookup: 250
    • HenkH: 216

    The unrolled version is:

    long _value = 0;
    byte _number = 7;
    
    _value = (_value + _number) << 8;
    _value = (_value + _number) << 8;
    _value = (_value + _number) << 8;
    _value = (_value + _number) << 8;
    _value = (_value + _number) << 8;
    _value = (_value + _number) << 8;
    _value = (_value + _number) << 8;
    _value = (_value + _number) << 8;
    

    The unsafe version is:

    long _value = 0;
    byte _number = 7;
    
    byte* p = (byte*)&_value;
    
    *p++ = _number;
    *p++ = _number;
    *p++ = _number;
    *p++ = _number;
    *p++ = _number;
    *p++ = _number;
    *p++ = _number;
    *p++ = _number;
    

    Sadly not performing 🙁

    The lookup is just a read to an array.

    All compiled for x64/release.

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

Sidebar

Related Questions

I need a few lines of Javascript code that will take a hexadecimal number
Does a application in .NET need to be built in 64 bit to take
I know that I can perform divide by 2 using right shift. For simplicity,
I need to create a bit of Javascript that can search inputted HTML from
I have a number of different data sources that I need to query and
Need to take a SELECT drop down list options and find if any of
I need to take offline and offline JOBS of schema B while I'm logged
I need to take a large file, with lines such as: member: cn=user0001,ou=people And
i need to take some row. They came from sql TARIH (sql column) is
I need to take a C++ vector with potentially a lot of elements, erase

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.