Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C Determine endianess

Name: Anonymous 2007-06-27 10:43 ID:Uo4E/xRf

I wrote it and i edited wikipedias article which had a terrible code and added mine, but i'm still not sure about my code, is it SCALABLE ENOUGH?

#include <stdio.h>

int main() {

        union {
                int s;
                char c[sizeof(int)];
        } un;
        const char *endianess[[] = { "big endian", "little endian" };

        un.s = (~(int)0) ^ (~(char)0);
        puts(endianess[!un.c[0]]);

        return 0;
}

Name: Anonymous 2007-06-27 10:46 ID:Heaven

OP here just a typo there, i have endianess[[] instead of endianes[]

Name: Anonymous 2007-06-27 10:56 ID:ZpgE8aWN

This solution is faster than machine code:

#include <stdio.h>

int main (void)
{
    short a = 1;
    printf("%s endian", *(char*) &a ? "Little" : "Big");
    return 0;
}

Name: Anonymous 2007-06-27 11:15 ID:MGu9Ag1G

and op's ip is .... 85.73.220.63

Name: Anonymous 2007-06-27 12:21 ID:Uo4E/xRf

>>4
heh :P

Name: Anonymous 2007-06-27 12:27 ID:v3LQ4OCt

Ahh clever.

Name: Anonymous 2007-06-27 14:13 ID:NAlubO+I

>>1
Oh, so you're Greek

Name: Anonymous 2007-06-27 14:48 ID:ysL8Ns/j

>>1
undefined behavior, see annex k of the C 89 standard.

Name: Anonymous 2007-06-27 14:49 ID:ysL8Ns/j

....oh and incase you actually didn't know


% cat > test.c
__LITTLE_ENDIAN__
__BIG_ENDIAN__
^C
% gcc -E test.c 
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "test.c"
1
__BIG_ENDIAN__

Name: Anonymous 2007-06-27 17:47 ID:Uo4E/xRf

>>9
you're supposed to use ^D .. anyway that's not a run-time check

>>7
oh you must be a leet hax0r

Name: Anonymous 2007-06-27 18:03 ID:Heaven

Executables are platform-bound, so doing the check at runtime adds absolutely nothing. OP is a fucking retard.

Name: Anonymous 2007-06-27 18:41 ID:ysL8Ns/j

>>10
It makes no different whether you use ^C or ^D you arent supposed to use either one, you fucking idiot.

Name: Anonymous 2007-06-27 19:02 ID:Uo4E/xRf

>>12
^D is correct, ^C is not

Name: Anonymous 2007-06-27 20:06 ID:K2AuVMv6

>>12
Not with line buffering, but not even yoshi can save your data if you're on a system that doesn't have that behavior and you hit ^C instead of ^D.

>>13
Unless you're on Windows, then it's ^Z-Enter. (Stupid Windows.)

Name: Anonymous 2007-06-27 20:13 ID:K2AuVMv6

oh, missed >>11
Suppose you're doing this during an auto-configuration script to test which platform to build an executable with.

Name: Anonymous 2007-06-27 20:46 ID:Heaven

>>15
What kinds of shitty compiler needs you to tell it the byteorder of the current machine?

(Also, show me a 'scripting' language that allows pointer/union tricks, but doesn't have a constant that tells you the byteorder.)

Name: Anonymous 2007-06-27 21:59 ID:2m8+eLJ2

makefile/build-process should provide a preprocessor macro of what endianness a long with all the other platform-dependant nonsense

Name: Anonymous 2007-06-27 23:39 ID:K2AuVMv6

>>16
Lots of algorithms are byte-order-dependent, and moreover, they need to be compiled with different compilers on different systems with different endianness. Sure, many compilers identify endianness in some manner, but it's far from portable, and often the simplest route to take is to figure it out for yourself.

It might seem stupid to start with, but every once in a while, it proves to be an extremely handy tool to have strapped on your hacker belt.

Name: Anonymous 2007-06-27 23:43 ID:mnQcz8y0

It still doesn't make any sense to do it at runtime, since the machine isn't going to change its endianness depending on whether it's executing the compiler or your program.

Name: Anonymous 2007-06-27 23:44 ID:ysL8Ns/j

>>18
no, none are... just the implementations might be.

Name: Anonymous 2007-06-28 3:05 ID:Heaven

>>19
*facepalm*
WHAT OTHER TIME WOULD YOU DO IT?

Name: Anonymous 2007-06-28 10:35 ID:Heaven

#include <stdio.h>
int main(){
#if __BYTE_ORDER == __LITTLE_ENDIAN
 puts("Little endian");
#elif __BYTE_ORDER == __BIG_ENDIAN
 puts("Big endian");
#else
 puts(1&'\1'?"Little endian":"Big endian");
#endif
 return 0;
}

Name: Anonymous 2007-06-28 12:39 ID:CBJzf1rA

forgive me for being a newfag not worthy of looking upon SICP, but what is the ~ operator?

Name: Anonymous 2007-06-28 12:40 ID:ixfFpgl3

>>23
Bitwise NOT

Name: Anonymous 2007-06-28 13:56 ID:Z7E2RyCE

>>24
bitwise COMPLEMENT asshole

Name: Anonymous 2007-06-28 22:59 ID:QrORhhnt

>>25
bitwise ONES COMPLEMENT asshole

Name: Anonymous 2007-06-28 23:01 ID:Z7E2RyCE

>>26
ah, thanks for the correction.

Name: Anonymous 2007-06-29 10:16 ID:7S0dHPUK

It performs a logical NOT, bit by bit, therefore bitwise NOT is also correct, asshole

Name: Anonymous 2007-06-29 12:36 ID:Mp8DF1ra

>>28
You are NOT right.

Name: Anonymous 2007-06-29 14:12 ID:/zM7+DqO

>>29
I lol'd .. NOT

Name: Anonymous 2007-06-29 14:25 ID:e3Wq01rn

>>30
>>29
I'VE NOT NOT LOL'D

Name: Anonymous 2007-06-29 14:36 ID:zkmispEM

#include <stdio.h>
int main(){
 float a=0;
 float b=a*~(int)a;
 puts(a==b?"a==b":"a!=b");
 printf("~*(int*)&a=%d ~*(int*)&b=%d\n",~*(int*)&a,~*(int*)&b);
 return 0;
}

Name: Anonymous 2007-06-29 21:03 ID:+ZS6femb

PROTIP: MOST PEOPLE IN THIS THREAD DON'T KNOW THE DIFFERENCE BETWEEN BITWISE NOT AND ONES COMPLEMENT.

Name: Anonymous 2007-06-30 13:42 ID:YlHqjaeq

>>33
PROTIP: LEARN HOW TO USE PROTIP

Name: Anonymous 2007-06-30 14:00 ID:zQsnOZDR

>>33
PROTIP: NO SUCH DIFFERENCE EXISTS.

Name: Anonymous 2007-06-30 14:23 ID:YlHqjaeq

>>35
PROTIP: YOU LEARN HOW, TOO

Name: Anonymous 2007-07-01 2:19 ID:Mfs6v72N

>>36
PROTIP: NUDIST PANTS CAKES.

Name: Anonymous 2007-07-01 6:30 ID:dHKtyUEu

PROTIP: PROTIP

Name: Anonymous 2007-07-01 12:38 ID:42aoosbW

PROTIP: MOST PEOPLE IN THIS THREAD DON'T KNOW HOW TO USE PROTIP

Name: Anonymous 2007-07-01 12:40 ID:Heaven

SICPTIP: MY OTHER CAR IS A CDR.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List