473,406 Members | 2,451 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,406 software developers and data experts.

bitwise operator and endianness

Hi Everyone,

I have a program unit which does >and << of an integer which is of
4 bytes length. The logic of shifting and action based on the result,
assumes that the system is big-endian.

Accordingly, if i need the program to work fine in a little-endian
system. I understand that the code needs to be changed. ( I couldn't
find any statement in C90 about endianness, hence i'm assuming that c
programs are not portable if the endianness of the system changes)

I wanted to know if the above holds true for bitwise and (&) and
bitwise or (|). I think, the system should take care of the
operation a&b or a|b irrespective of the endianness of the system.

Please provide your comments.

Thanks in advance!!!

Nov 5 '07 #1
5 6768
Rahul wrote:
Hi Everyone,

I have a program unit which does >and << of an integer which is of
4 bytes length. The logic of shifting and action based on the result,
assumes that the system is big-endian.
The appropriate fix depends upon the way in which that assumption is
built into the code. Endianness only matters if a) you're sharing data
between systems with different endianness or b) you're accessing the
integer as a array of unsigned char. Could you indicate exactly how the
code makes that assumption? Please give actual code, but simplified down
to it's essential elements.
Nov 5 '07 #2
Rahul wrote:
I have a program unit which does >and << of an integer which is of
4 bytes length. The logic of shifting and action based on the result,
assumes that the system is big-endian.
Well, no, I rather suspect it doesn't. What it /does/ depend on is
how you've chosen to represent the fields in the integer, and how
you get them in there.

How are you representing the fields in the integer, and how do
you get them in there? Show real, minimal, code.

--
Chris "rational code would do" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Nov 5 '07 #3
Rahul wrote:
>
Hi Everyone,

I have a program unit which does >and << of an integer which is of
4 bytes length. The logic of shifting and action based on the result,
assumes that the system is big-endian.
No it doesn't.
Any positive value, right shifted by one, has the same result
as if that positive value were divided by two instead.

--
pete
Nov 5 '07 #4
Rahul wrote:
Hi Everyone,

I have a program unit which does >and << of an integer which is of
4 bytes length. The logic of shifting and action based on the result,
assumes that the system is big-endian.

Accordingly, if i need the program to work fine in a little-endian
system. I understand that the code needs to be changed. ( I couldn't
find any statement in C90 about endianness, hence i'm assuming that c
programs are not portable if the endianness of the system changes)
[...]
Thought experiment:

int i = 42;
int j = i * 2;
int k = i << 1;
assert (j == k);

Can the assert() fail? If so, what does it tell you about
the system's endianness? If not, what does it tell you about
the *influence* of the system's endianness?

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 5 '07 #5
Rahul wrote:
Hi Everyone,

I have a program unit which does >and << of an integer which is of
4 bytes length. The logic of shifting and action based on the result,
assumes that the system is big-endian.
The shift operators act on values, not bit patterns. This may seem
confusing, I know. But the fact is that if you depend on the particular
form of storage, then you are dealing with implementation-specific
behavior.

Similarly, if you depend on, for example, using a specific size unsigned
integer to shift left and then right to zero out the more significant
bits, your program will not be portable. Nor will it be portable if you
attempt to shift values greater than the equivalent of the number of
bits in the type (or by negative numbers of bits). Nor will it be
portable if the left operand is a negative signed value and the right
operand is non-zero.

Now, "4 bytes length" is not useful information here. If CHAR_BIT is 8,
then the relevant value is "32 bits". The only types for which << and
>are defined for all values of the left operand (being 32 bits) are
unsigned long and unsigned long long. So your integer must not be of
type [signed or unsigned] x [char or short or int], nor can it be
possibly negative and signed long or signed long long.
Accordingly, if i need the program to work fine in a little-endian
system. I understand that the code needs to be changed. ( I couldn't
find any statement in C90 about endianness, hence i'm assuming that c
programs are not portable if the endianness of the system changes)
If the _values_ are the salient feature of your integers, then your
programs will be portable as long as
1) the right operand is no longer than the minimum possible width of
the left operand and positive, and
2) the the left operand for >is never a negative signed value with a
non-zero right operand.
>
I wanted to know if the above holds true for bitwise and (&) and
bitwise or (|). I think, the system should take care of the
operation a&b or a|b irrespective of the endianness of the system.
But, if you want your code to be portable, use only unsigned operands.
And remember that any relevant testa should, in the usual case, not
require that the type of the tested operand be wider than the minimum
guaranteed by the standard.

Nov 5 '07 #6

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

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
5
by: Bill Dee | last post by:
I need help converting a tiny piece of code which uses the bitwise complement operator from C# to VB.NET. In C# I currently have this: long useThis = Myclass.ALLCONSTANTS; long doNotUse =...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
3
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should...
17
by: zirconx | last post by:
I'm trying to understand how the bitwise AND can be used. I've read about what it does but am having trouble applying it practically. I'm working on a system that somebody else wrote and they...
5
by: Junmin H. | last post by:
Hello, all. I never use them when I am programming. But i have been reading codes that using them. When should I use them? Why not use char/short/int/long? Is it very important to know them well?...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.