Even more ways to determine endianness at runtime

Earlier, I posted a method of determining the host’s endianness at runtime using pointer trickery. As with many things, there are other ways of achieving this, some more or less desirable in particular circumstances.

One such alternative is using unions and preprocessor directives, outlined below:

#include <stdint.h>

int main (int argc, const char * argv[]) {
#pragma pack(1)
    union { uint16_t number; uint8_t bytes[2]; } babe;
#pragma pack()
    babe.number = 0xBABE;

    uint8_t first = babe.bytes[0];
    return (first == 0xBA);
How to: Detecting endianness programmatically in a C++ program

How to: Detecting endianness programmatically in a C++ program

Detecting endianness programmatically in a C++ program

Is there a programmatic way to detect whether or not you are on a big-endian or little-endian architecture? I need to be able to write code that will execute on an Intel or PPC system and use exactly the same code (i.e. no conditional compilation).

Answer: Detecting endianness programmatically in a C++ program

As pointed out by Coriiander,…

View On WordPress

Determining endianness at runtime

A co-worker of mine was recently lucky enough to score a position with Apple as a software developer. During the (fairly extensive, from what I’ve heard) interview process, he was asked a number of technical questions.

One question that he confessed that he did not know the answer to interested me and it is the title of this post. “Most people” (at least those that would read a blog like this) know that x86 is little-endian and some people know that the PowerPC is big-endian. PowerPC is, in fact, a mixed-endian machine that is capable of switching endian modes at runtime, a fact I learned while tinkering with my Xbox 360 — which has a PPC-based tri-core Xenon processor.

So how do we determine endianness at runtime? Seasoned OS X developers are already well-aware of NSHostByteOrder(), CFByteOrderGetCurrent() and their respective enums. Perhaps those are the answers that Apple were looking for, but that’s boring. Working it out ourselves is a trivial matter in C:

#include <stdint.h>

int main (int argc, const char * argv[]) {
    const uint16_t babe = 0xBABE;
    uint8_t first = *(uint8_t *)&babe;
    return (first == 0xBA);

We can compile and execute this program:

$ cc endian.c
$ /a.out
$ echo $?

0 will be returned on a little-endian system and 1 otherwise.

Not that I consider big-endian to be a failure :)

How to: How do I convert between big-endian and little-endian values in C++?

How to: How do I convert between big-endian and little-endian values in C++?

How do I convert between big-endian and little-endian values in C++?

How do I convert between big-endian and little-endian values in C++?

EDIT: For clarity, I have to translate binary data (double-precision floating point values and 32-bit and 64-bit integers) from one CPU architecture to another. This doesn’t involve networking, so ntoh() and similar functions won’t work here.

EDIT #2: The…

View On WordPress

C code to check your system's endianness

/* if you don’t know what is “Endianness”, here is a wikipedia article about the topic: Endianness */

#include <stdio.h>
int main(){
    unsigned short s = 0x0001;
    unsigned char c;

    /*point to ‘s’ using a char pointer, then dereference it, this will access the first byte of ‘s’ and copy it to ‘c’.*/

    c = *(unsigned char *) &s;

    if(c == 0x01){
        printf(“this is a little endian system.\n”);
        printf(“this is a big endian system.\n”);
    return 0;

"On Holy Wars and a Plea for Peace" by Danny Cohen ends with: "Swift’s point is that the difference between breaking the egg at the little-end and breaking it at the big-end is trivial. Therefore, he suggests, that everyone does it in his own preferred way. We agree that the difference between sending eggs with the little- or the big-end first is trivial, but we insist that everyone must do it in the same way, to avoid anarchy. Since the difference is trivial we may choose either way, but a decision must be made."

lumi@Kagamine-Rin:~$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                16
On-line CPU(s) list:   0-15
Thread(s) per core:    2
Core(s) per socket:    4
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 26
Stepping:              5
CPU MHz:               1596.000
BogoMIPS:              4533.44
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              8192K
NUMA node0 CPU(s):     1,2,4,6,8,10,12,14
NUMA node1 CPU(s):     0,3,5,7,9,11,13,15


learning bout exporting bet and highest quality file possible

create in and out point 

Q.T (quicktime)   .mov    lossless

uncompressed 10-bit 4:2:2

frame rate - 25 fps

1920 x 1080  overscan     video safe area

sound is 48 KHz for DVD audio    16 bits   little endian

un-tick prepare for internet streaming

thespectralboy replied to your postGaming suggestions at 4:00am. GO.


I have actually always wanted to play this! Sadly I do not own a copy and I think my friend Darren sold his copy. One day! I am determined to play this.

endianness replied to your postGaming suggestions at 4:00am. GO.

sleeping game? what that doesn’t even exist

My body doesn’t know how to sleep during the night. I’m fucking nocturnal. And sick.

Resolved: Unicode, Unicode Big Endian or UTF-8? What is the difference? Which format is better? #computers #solution #fix

Resolved: Unicode, Unicode Big Endian or UTF-8? What is the difference? Which format is better? #computers #solution #fix

Unicode, Unicode Big Endian or UTF-8? What is the difference? Which format is better?

When I try to save a text file with non-English text in Notepad, I get an option to choose between Unicode, Unicode Big Endian and UTF-8. What is the difference between these formats?

Assuming I do not want any backward compatibility (with older OS versions or apps) and I do notcare about the file size, which…

View On WordPress

พูดถึง VarInt และวิธีนับ byte ผ่าน ByteArrayOutputStream

VarInt ของ Google นีก็ไม่ใช่ว่าจะเลวร้ายซะทีเดียว ก็ดีอยู่บ้างในเมื่อเราส่งข้อมูลที่เป็นไปได้ทั้งขนาดใหญ่และขนาดเล็ก เช่นเราจะส่งแค่เลข 10 ไปเราใช้แค่ 1 byte แต่ถ้าเราส่ง 257 ไปต้องใช้ 2 bytes ซึ่ง VarInt จะอ่านได้หมด

หลักการทำงานของ VarInt ง่ายๆ ก็คือแต่ละ byte ใน bit ลำดับที่ 8 จะมีสัญลักษณ์ ไม่สิ เป็น boolean ที่บอกว่าต้องอ่านต่อไปหรือไม่ หากเป็น 1 หรือ true ก็อ่านต่อไป จนไปเจอ 0 หรือ false ก็หยุด และ VarInt จะเอาหลักที่น้อยที่สุดส่งมาก่อน (least significant group) ถ้าเทียบก็นึกถึง little-endian เอาก่อนก็ได้ ผมคิดว่ามันก็คล้ายกันคือเอา byte ที่มีค่าน้อยที่สุดเอามาไว้หน้าสุด

ByteArrayOutputStream มันก็เป็น class ที่มีประโยชน์มากๆ บางคนอาจไม่คิดว่ามันนับความยาวของ byte array ได้ง่ายๆ โดยไม่ต้องไปคำนวณอะไรแล้วออกเป็นความยาว สะดวกกว่าและถูกต้องกว่า แต่ถ้าเรื่องเปลืองและช้านี่ก็เป็นไปได้ เรื่องนี้เจอระหว่างอ่านวิธีการอ่านสถานะเซิฟเวอร์ผ่าน Java แล้วก็ได้นี่มา เพราะตอนแรกผมทำผิดเพราะนับความยาว byte array ผิด 555555555

Apparently I don’t know anybody who owns a PS4 so if any of my followers feel like adding me my username is endianness. Just uh, send me your Tumblr username so I know who you are.