473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

are bit manipulation guaranteed to work endian independent

Provided a unsigned integer say (32bits b31-b0 bits) and its required
to get (b19-b10) .

One approach is have a mask and right shift it

#define BIT_MASK 0x000FFC00

uint32 variable;

variable &= BIT_MASK
variable >>= 10

My question is, does the above code guarantee to do the same in big
and little endian machines? Or should i make sure to check the
endianess and manipulate in accord?
What is the general approach to guarantee bit manipulations on a
software works fine irrespective to endianess of h/w underneath?

Mar 15 '06 #1
4 4384

senthil...@gmail.com wrote:
Provided a unsigned integer say (32bits b31-b0 bits) and its required
to get (b19-b10) .

One approach is have a mask and right shift it

#define BIT_MASK 0x000FFC00

uint32 variable;

variable &= BIT_MASK
variable >>= 10

My question is, does the above code guarantee to do the same in big
and little endian machines? Or should i make sure to check the
endianess and manipulate in accord?
What is the general approach to guarantee bit manipulations on a
software works fine irrespective to endianess of h/w underneath?


The above will work irrespective of endinanness. Shifts and bit
manipulations are guaranteed to work as you'd expect.

The endianness only comes into play when it comes to storage (e.g.
reading/writing files).

--
BR, Vladimir

Mar 15 '06 #2

<se********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Provided a unsigned integer say (32bits b31-b0 bits) and its required
to get (b19-b10) .

One approach is have a mask and right shift it

#define BIT_MASK 0x000FFC00

uint32 variable;

variable &= BIT_MASK
variable >>= 10

My question is, does the above code guarantee to do the same in big
and little endian machines?
Yes and no. It depends on where the value of 'variable' came from.

If the value of variable originated in your program, yes.
variable=0xFFAA55EE;
It's endianess _within_ your program transparent to you. Both little and
big endian will function correctly and identically, although the byte order
is different.

If the value of variable originated in elsewhere, say a jpeg, a zip, or
network data, no.
The values will be swapped if the transfer of data was between a big and
little endian machine.
The values will be the same if the transfer of data was from a big to a big,
or from a little to a little endian machine (with a couple of rare
exceptions).
Or should i make sure to check the
endianess and manipulate in accord?
Are the values in variable being transfered between different type machines?
(jpeg, zip, tcp/ip)
If so, you'll need to correct for endianess.
What is the general approach to guarantee bit manipulations on a
software works fine irrespective to endianess of h/w underneath?


Usually, a number of macros are or a union is defined in order to switch
around the byte order of the data.
Rod Pemberton
Mar 15 '06 #3
Thanks for your replies.
So files are the major concern since its a stored data in memory and
needs to be ensured about endianess.
I know network data are network byte ordered(i think little endian)
so i remember using network to host order conversion function to patch
up.

Mar 15 '06 #4
<se********@gmail.com> wrote in message
news:11********************@j52g2000cwj.googlegrou ps.com...
Thanks for your replies.
So files are the major concern since its a stored data in memory and
needs to be ensured about endianess.
I know network data are network byte ordered(i think little endian)
so i remember using network to host order conversion function to patch
up.


network byte order is "big-endian".
Mar 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Peter Pfeiffer | last post by:
I've written several scripts that have "while" blocks which increment a date by one day if the date does not match one of a group of dates. However, sometimes it apparently steps out out the while...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
2
by: Mehta Shailendrakumar | last post by:
Hi, I am sending this question again as new question rather than reply to old question Please refer below: struct raw_data { unsigned char x; unsigned char y; }; union full_data
2
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
0
by: ghselcuk | last post by:
Hi, I converted my Java codes to C#. In the code i posted a request to a server as bytes. In Java there was no problem but in C# i get error from the server. I write bytes in a stream. My...
30
by: mellyshum123 | last post by:
I'm wanting to write an int to a file, and so far I have written: const char buff; int num = 256 //for the sake of the example sprintf(buff, "%d", num); Now my question is what to do next. I...
33
by: raghu | last post by:
Is it possible to know whether a system is little endian or big endian by writing a C program? If so, can anyone please give me the idea to approach... Thanks a ton. Regards, Raghu
1
by: vikas1008 | last post by:
hi firends, How can I write a simple C code using pointers that can run on both big endian and little endian machines.
15
by: DL | last post by:
say, we have the following: // declare an array myArray = ; // short hand? same as myArray = new Array ? // populate it myArray = 0; myArray = 0; myArray = 1; myArray = 0;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.