Is there any "standard" htonl-like function for 64 bits integers in C++?

is there anyone or are there anyone
are there any 's
are there any questions
are there any other
is there any comments
any grammar
any singular or plural
is there any problem

I'm working on an implementation of the memcache protocol which, at some points, uses 64 bits integer values. These values must be stored in "network byte order".

I wish there was some uint64_t htonll(uint64_t value) function to do the change, but unfortunately, if it exist, I couldn't find it.

So I have 1 or 2 questions:

  • Is there any portable (Windows, Linux, AIX) standard function to do this ?
  • If there is no such function, how would you implement it ?

I have in mind a basic implementation but I don't know how to check the endianness at compile-time to make the code portable. So your help is more than welcome here ;)

Thank you.


Here is the final solution I wrote, thanks to Brian's solution.

uint64_t htonll(uint64_t value)
{
    // The answer is 42
    static const int num = 42;

    // Check the endianness
    if (*reinterpret_cast<const char*>(&num) == num)
    {
        const uint32_t high_part = htonl(static_cast<uint32_t>(value >> 32));
        const uint32_t low_part = htonl(static_cast<uint32_t>(value & 0xFFFFFFFFLL));

        return (static_cast<uint64_t>(low_part) << 32) | high_part;
    } else
    {
        return value;
    }
}

You are probably looking for bswap_64 I think it is supported pretty much everywhere but I wouldn't call it standard.

You can easily check the endianness by creating an int with a value of 1, casting your int's address as a char* and checking the value of the first byte.

For example:

int num = 42;
if(*(char *)&num == 42)
{
   //Little Endian
}
else
{
   //Big Endian
} 

Knowing this you could also make a simple function that does the swapping.


You could also always use boost which contains endian macros which are portable cross platform.

Connotations of "Is there any" vs "Are there any", With "is there any" it implies to me that the speaker is asking for one solution; a " magic bullet" type of Chrome plugin to use your example. They're asking for a� Is there any student willing to sing a Mozart aria in front of the class? You must need glasses. Take a look at that table once again. Now, tell me. Is there any apple on that table? Is there any bird that is known to have no feathers? The sentences in the latter group may be stated in the plural as well. CJ

#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))

The test (1==htonl(1)) simply determines (at runtime sadly) if the hardware architecture requires byte swapping. There aren't any portable ways to determine at compile-time what the architecture is, so we resort to using "htonl", which is as portable as it gets in this situation. If byte-swapping is required, then we swap 32 bits at a time using htonl (remembering to swap the two 32 bit words as well).


Here's another way to perform the swap that is portable across most compilers and operating systems, including AIX, BSDs, Linux, and Solaris.

#if __BIG_ENDIAN__
# define htonll(x) (x)
# define ntohll(x) (x)
#else
# define htonll(x) ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
# define ntohll(x) ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
#endif

The important part is to use __BIG_ENDIAN__ or __LITTLE_ENDIAN__; and not __BYTE_ORDER__, __ORDER_BIG_ENDIAN__ or __ORDER_LITTLE_ENDIAN__. Some compilers and operating systems lack __BYTE_ORDER__ and friends.

Is there any vs. Are there any, The two sentences do go together, and are grammatical, but they would only occur in the particular circumstance of persistent enquiry. Get a� There are no games that match your filter or search query. IsThereAnyDeal.com is not responsible for the content displayed. Any information may be already outdated at the time of visit.

You can try with uint64_t htobe64(uint64_t host_64bits) & uint64_t be64toh(uint64_t big_endian_64bits) for vice-versa.

When should I use 'is there any' or 'are there any' in English?, It would depend on what follows, namely, either a singular noun or a plural noun. Eg. 1 — Is there any bread? is there any food? is there any cheese? There are no Specials that match your selection. IsThereAnyDeal.com is not responsible for the content displayed. Any information may be already outdated at the time of visit.

This seems to work in C; did I do anything wrong?

uint64_t htonll(uint64_t value) {
    int num = 42;
    if (*(char *)&num == 42) {
        uint32_t high_part = htonl((uint32_t)(value >> 32));
        uint32_t low_part = htonl((uint32_t)(value & 0xFFFFFFFFLL));
        return (((uint64_t)low_part) << 32) | high_part;
    } else {
        return value;
    }
}

'Is there any' or 'Are there any', Yes , and one other thing , I've read that if a noun is countable we must put the noun in plural after 'any' which means "Is there any difference" is� Please let me know if there is any problem. Please let me know if there are any problems. If there is any book that is better than yours, it is this one. If there are any books that are better than yours, it is this one. If there is any scooter or bike parked in front my door, I will throw it away.

To reduce the overhead of the "if num == ..." Use the pre-processor defines:

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#else
#endif

Is There Any Vs Are There Any?, Do native speakers say like this?? Is it grammatically correct? Do native speakers say it like this? or Do native speakers say this? Answer: Not� Any gatherings outside the household and vacation rentals are also prohibited. Yes, there is a strict 14 day Hawaii quarantine rule (see above).

Any - English Grammar Today, There aren't any clean knives. They're all in the dishwasher. (+ plural noun). Warning: We don't use any with this meaning with singular countable nouns:. The latest hurricane watches and warnings for the Atlantic Basin. Hurricane tracking maps, current sea temperatures, and more.

There is, there's and there are, Not: It's a letter on your desk. There are three Japanese students in my class. There is and there's are both singular forms. We use there's more� There have been 1, 219 confirmed cases of the coronavirus in the country, according to the Ministry of Health's release. There are currently 23 people who have reported active cases of the disease

There Is There Are - English Grammar Rules, Positive Sentences � There is milk in the fridge. � There is some sugar on the table . � There is ice cream on your shirt.

Comments
  • possible duplicate of this one stackoverflow.com/questions/809902/64-bit-ntohl-in-c
  • @ereOn: I also have similar question here. If possible can you take a look and let me know what wrong I am doing here?
  • Instead of including your answer inside the question, you should let your answer with answers. It is more readable.
  • Thanks ;) The boost macros seem interesting. Do you have link ?
  • Technically, just a byte-swap is not always enough. There are systems that are middle-endian (though no modern systems are). Also, bswap_64 is definitely NOT supported everywhere; just look at the long list of systems with bswap_64 missing: gnu.org/software/gnulib/manual/html_node/bswap_005f64.html
  • Note that bswap_64 is not POSIX compliant.
  • bswap_64 is not present on Solaris i86pc. Does anyone know if it is present on AIX?
  • I'm sure this answer is great, but could you please add something other than code to explain what and why?
  • Thanks! I've edited your answer with the explanation.
  • as mentioned @ereon you cannot use 32 bits shifts on ntohl or ntohl because they return uint32_t, hence triggering UB.