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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:44:41+00:00 2026-05-27T23:44:41+00:00

I’m working on a program that deals with a file that uses hashes. The

  • 0

I’m working on a program that deals with a file that uses hashes. The data is divided into blocks length 0x1000. I need to calculate the amount of blocks a segment with a certain starting and ending offset covers.

For example, if the starting offset of the segment was 0x2000 and the ending was 0x3523, it would mean that it takes up two blocks, 0x2000 and 0x3000. It doesn’t take up the full 0x2000 bytes in the data blocks, but it’s considered to “take up a block” when it’s inside it. My first thought was to do:

( ( EndingOffset - StartingOffset ) + 0xFFF ) >> 0xC

This is the equivalent of Math.Ceil((EndingOffset - StartingOffset) / 0x1000), but I’m new to bitwise operators and like the challenge of working with them.

Anyway, the logic was flawed, as, and this is the case that got me, if the starting offset is 0x3D8A and the end offset 0x671D, the difference between the two is 0x2993. Rounded up that’s 0x3000, or three blocks. The segment actually takes up four, 0x3000, 0x4000, 0x5000, and 0x6000. So my next train, and unfortunately my last, was to instead find the difference between the the offset of the first block the segment is in, and the offset of the first block the segment isn’t in.

With 0x3D8A and 0x671D, this brings me to (0x7000 - 0x3000) >> 0xC, which successfully yields the correct amount of blocks, 4. The way I wrote it is what I want to improve, which is:

BlockSize = ((((OffsetEnd + 0xFFF) >> 12) + 1) - ((OffsetStart + 0xFFF) >> 12));

I know I’ve over-complicated a simple problem, but I can’t wrap my little brain around how to write it better.

edit: That’s embarrassing. I don’t know how I came to that instead of

(((OffsetEnd + 0xFFF) >> 12) - (OffsetStart >> 12));

Still doesn’t seem complete though.

edit 2: Sorry, forgot to mention that the ending offset is exclusive, not inclusive and is the position after the last byte of the segment meaning:

(((OffsetEnd - 1 + 0xFFF) >> 12) - (OffsetStart >> 12));

edit 3: Going off of Kerek’s answer, I end up with:

BlockSize = 1 + (offsetEnd - 1 >> 12) - (offsetStart >> 12);

..or, with counting from 0,

BlockSize = (offsetEnd - 1 >> 12) - (offsetStart >> 12);

edit 4: Forget the counting from zero, I’m sticking with:

BlockSize = 1 + (offsetEnd - 1 >> 12) - (offsetStart >> 12);
  • 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-27T23:44:41+00:00Added an answer on May 27, 2026 at 11:44 pm

    Assuming that your data ranges are given as [start, end), it’s quite simple:

    unsigned int start_block = start_offset / 0x1000;
    unsigned int end_block = end_offset / 0x1000;
    
    unsigned int number_of_blocks = end_block - start_block + (end_offset > start_offset && end_offset % 0x1000 > 0 ? 1 : 0);
    

    The bitfiddling equivalent of / 0x1000 is >> 12, I suppose.

    Your data will occupy the block range [start_block, end_block). Note that block 0 is [0x0000, 0x1000), block 1 is [0x1000, 0x2000)`, etc.

    As @Ron pointed out, you’ll need one conditional to distinguish whether the last (“one-past”) block is empty or not, and increment the block count by one in the latter case.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
In my XML file chapters tag has more chapter tag.i need to display chapters
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,

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.