473,378 Members | 1,119 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,378 software developers and data experts.

Endians and c++

Hi

Is it endian-safe to use bit operations in C++ ?

For example bit-shifts, does the left shift by n always equal
multiplicication by 2^n (modulo 2^integerBits), independent of endian
system used ?
Jul 23 '05 #1
5 1402
Kalle Rutanen wrote:
Is it endian-safe to use bit operations in C++ ?

For example bit-shifts, does the left shift by n always equal
multiplicication by 2^n (modulo 2^integerBits), independent of endian
system used ?


Yes. The bit shifts operate on the number _value_, the representation
of the number in memory has nothing to do with it.

V
Jul 23 '05 #2

Kalle Rutanen wrote:
Hi

Is it endian-safe to use bit operations in C++ ?

For example bit-shifts, does the left shift by n always equal
multiplicication by 2^n (modulo 2^integerBits), independent of endian
system used ?


No, but that's actually unrelated to endianness. Multiplication by
2^-1 is well defined, but a left shift over -1 bits isn't.
Overflow is undefined for signed types.

For unsigned types and positive n, the result is in fact defined as if
done by multiplication or division. (5.8 Shift operations)

HTH,
Michiel Salters

Jul 23 '05 #3
msalters wrote:
Kalle Rutanen wrote:
Hi

Is it endian-safe to use bit operations in C++ ?

For example bit-shifts, does the left shift by n always equal
multiplicication by 2^n (modulo 2^integerBits), independent of endian
system used ?

No, but that's actually unrelated to endianness. Multiplication by
2^-1 is well defined, but a left shift over -1 bits isn't.


Wouldn't left shift by a negative number be the same as right shift by
the absolute value of the number? I mean, theoretically, of course.
[...]


V
Jul 23 '05 #4
Thanks for both repliers!
Jul 23 '05 #5

Victor Bazarov wrote:
msalters wrote:
Kalle Rutanen wrote:
Hi

Is it endian-safe to use bit operations in C++ ?

For example bit-shifts, does the left shift by n always equal
multiplicication by 2^n (modulo 2^integerBits), independent of endiansystem used ?

No, but that's actually unrelated to endianness. Multiplication by
2^-1 is well defined, but a left shift over -1 bits isn't.


Wouldn't left shift by a negative number be the same as right shift

by the absolute value of the number? I mean, theoretically, of course.


Yes, of course. If the shift operand is a literal or a constant, this
can
be an automatic transformation. However, if the rhs is a variable, this
cannot be done at compile-time. Now, apparently some CPU's dont't like
negative arguments, and it would be bad if they have have to implement
every << as (IF RHS<0 (NEG RHS, SHR RHS) ELSE SHL RHS ). This can be
done in C++ if needed, but you don't pay if you don't need it.

HTH,
Michiel Salters

Jul 23 '05 #6

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

Similar topics

19
by: Svennglenn | last post by:
I'm working on a program that is supposed to save different information to text files. Because the program is in swedish i have to use unicode text for ÅÄÖ letters. When I run the following...
27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
4
by: | last post by:
Assume that we have the following structure to communicate over a serial communication device (Rs232) or ethernet between two or many computers. There is no guaranty that computers have the same...
4
by: Gernot Frisch | last post by:
Hi, does the ARM processors have different endians than x86 platform? For ints and/or floating points??? I get a really strange objec when loading it from a file. Creating manually works. ...
2
by: Steven T. Hatton | last post by:
I'm trying to parse an ELF file to build a human readable representation. I know it's been done before, and there are tools such as objdump, nm, readelf, c++filt, etc. I can look at the source to...
7
by: Dave | last post by:
We are trying to migrate a MS SQL server app to DB2 8.1 Linux platform. Our database has got about 300+tables with total size - 150 GB We are using MS SQL's BCP utility to extract data from...
8
by: ma740988 | last post by:
Data stored on a storage device is byte swapped. The data is big endian and my PC is little. At issue: There's a composite type ( a header ) at the front of the files that I'm trying to read in....
1
by: manojdagar | last post by:
here now stands that small knot of villages known as the Endians, a mighty forest once stood. Indeed, legand has it that you could have stoodon the edge of the wood and seen it stretch out for miles,...
23
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.