Jump to content
Nytro

[C] Detectare procesor: big/little endian

Recommended Posts

Scrieti un mic programel C (cateva linii sunt de ajuns) care sa detecteze daca un procesor este little endian sau big endian. Cred ca stiti care e diferenta...

Nu e greu deloc, raspunsurile pe PM. :)

Au raspuns pana acum:

- Ellimist

- BGS

- H3xoR

- NemesisITSC

- Matei

Edited by Nytro
Link to comment
Share on other sites

Sa presupunem ca un int (in exemplul nostru) se memoreaza pe 4 octeti.

Pentru valoarea int x = 1; se va memora:

- big endian: 00 00 00 01

- little endian: 01 00 00 00

Singurul lucru care trebuie facut, e sa declaram un int (sau ceva ce se memoreaza pe mai mult de un octet), si sa citim primul octet.

Cam toate rezolvarile au avut aceasta idee la baza, cam aceasta ar fi rezolvarea:

#include <stdio.h>

int main()
{
int var = 1;
unsigned char *c = (unsigned char *)&var; /* Pointer la primul octet */

printf("Procesorul este %s\n", *c == 1 ? "little endian" : "big endian");

return 0;
}

Cei care au raspuns corect in primul post. Felicitari. :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...