Solved

Bit shifting, C/C++ macro conversion

Posted on 2000-02-16
9
256 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
ID: 2526664
Adjusted points to 150
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2526803
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
ID: 2526815
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 10

Expert Comment

by:Lischke
ID: 2526824
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
 

Author Comment

by:redhorn
ID: 2526855
>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
ID: 2526869
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
ID: 2526882
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
ID: 2526968
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
ID: 2526981
Good luck and thank you.

Ciao, Mike
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best Firemonkey component pack 1 105
indy Tmemory stream non latin charcter issue 2 111
Delphi Dbf export problem to a Visual Foxpro application 6 189
select query - oracle 16 100
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 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