473,568 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4398

senthil...@gmai l.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********@gma il.com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.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=0xFFAA 55EE;
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********@gma il.com> wrote in message
news:11******** ************@j5 2g2000cwj.googl egroups.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
2831
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 loop even though my condition isn't met. Will work for a few loops then steps out often. Are there javascript "date" issues? I have also noticed...
8
27445
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
9743
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
6539
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
1063
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 converter converts Java bytes to C# sbytes. But i can't write sbytes to a stream. I think it can be endian problem. How can i make the bytes endian...
30
1974
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 could use fwrite, but I don't understand how the size works, I'm not sure if it writes out the whole buffer or not, and I definetly only want to...
33
3204
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
3088
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
1731
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
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.