Solved

Bit shifting, C/C++ macro conversion

Posted on 2000-02-16
9
253 Views
Last Modified: 2010-04-04
The macros are from the PGP SDK, and are used to extract values from an encoded version number. Here is an excerpt from the PGP sources which sets up the problem:

====
/*
API version:
Top byte is major, next 3 nibbles minor, next 2 bug fix, last nibble reserved:  0xMMmmmrrR
      
example: 1.0.0 = 0x01000000
      
0x01000000 SDK 1.0.0
0x02000000 SDK 1.1.0
0x02000010 SDK 1.1.1
0x03000000 SDK 1.5
0x03000010 SDK 1.5.1
0x03000020 SDK 1.5.2
0x03001000 SDK 1.6
0x03002000 SDK 1.7
0x03002010 SDK 1.7.1
*/

#define kPGPsdkAPIVersion            ( (PGPUInt32)0x03002010 )
#define PGPMajorVersion( v )      ( ( ((PGPUInt32)(v)) & 0xFF000000 ) >> 24 )
#define PGPMinorVersion( v )      ( ( ((PGPUInt32)(v)) & 0x00FFF000 ) >> 16 )
#define PGPRevVersion( v )            ( ( ((PGPUInt32)(v)) & 0x00000FF0 ) >> 4 )

====

The last three are macros to extract the SDK version numbers from kPGPsdkAPIVersion; I've translated them into the following functions (PGPUint32 = Cardinal):

function PGPMajorVersion(const v): PGPUint32;
begin
  result := (PGPUint32(v) and $FF000000) shr 24;
end;

function PGPMinorVersion(const v): PGPUint32;
begin
  result := (PGPUint32(v) and $00FFF000) shr 16;
end;

function PGPRevVersion(const v): PGPUint32;
begin
  result := (PGPUint32(v) and $00000FF0) shr 4;
end;

The problem is that when passed '$03002010' these three functions return 3, 0, and 1, respectively, when I believe they should be returning 1, 7, and 1.

The obvious question is, Have I translated the macros properly? Though I think I understand what the macros are doing, I freely admit to having no experience with bit-shifting.

Can anyone suggest changes to the three functions so they will return the expected results, i.e., 1-7-1 from $03002010, 1-5-2 from $03000020, etc.?
0
Comment
Question by:redhorn
  • 5
  • 3
9 Comments
 

Author Comment

by:redhorn
Comment Utility
Adjusted points to 150
0
 
LVL 10

Expert Comment

by:Lischke
Comment Utility
I think you problem results from the untyped parameter. Try instead this:

function PGPMajorVersion(const v: PGPUint32): PGPUint32;
begin
  result := (v and $FF000000) shr 24;
end;


Ciao, Mike
0
 
LVL 2

Expert Comment

by:aubs
Comment Utility
Looking at the code I would expect that $03002010 should return:
3, 2 and 1

Which is surely correct:

"Top byte is major, next 3 nibbles minor, next 2 bug fix, last nibble reserved:  0xMMmmmrrR "

Aubs
0
 
LVL 10

Expert Comment

by:Lischke
Comment Utility
I have to make an addition. I think the results you got with your old version are correct. Look at

$03000000

which contains 24 null bits and corresponds therefor to 3 when shifted 24 bits to the right and not 1 as you expect. They say the top byte is major but obviously it doesn't directly correspond to the version number. From the table given it is so that a result of 3 implicitely corresponds to major version 1. This is not a value you can get from bit shifts. You can follow this by comparing the values of the first byte to the major version numbers in the table.

Ciao, Mike
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:redhorn
Comment Utility
>Looking at the code I would expect
>that $03002010 should return:
>3, 2 and 1

>Which is surely correct:

>"Top byte is major, next 3 nibbles >minor, next 2 bug fix, last nibble >reserved:  0xMMmmmrrR "

I agree, however the version number is known to be 1.7.1. The translations of the macros have looked right to me from the start, but they have never produced the supposedly correct result.

Based on the comments I have seen I am starting to think that the original macros are incorrect. They are not actually used anywhere in PGP, I've noticed.

0
 
LVL 10

Accepted Solution

by:
Lischke earned 150 total points
Comment Utility
Addtionally, there's no direct way to generally obtain the correct version number from the bit combinations mentioned above. For instance the 7 in 1.7.1 consists of 3 consecutive bits which never appear in the bit field.

Ciao, Mike
0
 
LVL 10

Expert Comment

by:Lischke
Comment Utility
Ah yes, they say the last 3 nibbles are for bug fixes and reserved, so the second macro needs a shift of 12 not 16. I'd say, just ignore them, the table shows nobody ever has used the scheme to encode the version.

Are you going to accept this as answer?

Ciao, Mike
0
 

Author Comment

by:redhorn
Comment Utility
I had already accepted your previous answer, as a matter of fact. Thank you. "Give it up" is ultimately the answer to my question, but knowing why I should give up helps. ;-)

I think there is some other kind of versioning at work here. I will go back to the documentation to see if there is something else that the output of these macros could refer to. It is true that they aren't used anywhere in PGP itself, but they have been in the sources for years now, and are even referred to in the docs. I must be missing something.
0
 
LVL 10

Expert Comment

by:Lischke
Comment Utility
Good luck and thank you.

Ciao, Mike
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now