Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Bit shifts and endianness (http://www.velocityreviews.com/forums/t440773-bit-shifts-and-endianness.html)

 gamehack 01-05-2006 08:11 PM

Bit shifts and endianness

Hi all,

I was thinking today, suppose we have the number
n = 0xAB 0xFF
which is equivalent to 44031 in decimal. In big endian it will be
stored as
10101011 11111111
but in little endian it will be
11111111 10101011
If we then apply a bit shift n << 2; that would give us completely
different numbers. So is actually a left bit shift done to the left on
a big-endian and left bit shift on little endian is actually a right
shift in the memory?

Thanks a lot

PS. What other problems arise from endianness? Are there any good
resources on that?

 Keith Thompson 01-05-2006 08:26 PM

Re: Bit shifts and endianness

"gamehack" <gamehack@gmail.com> writes:
> I was thinking today, suppose we have the number
> n = 0xAB 0xFF

That's two numbers. Do you mean 0xABFF?

> which is equivalent to 44031 in decimal.

Apparently so.

> In big endian it will be
> stored as
> 10101011 11111111
> but in little endian it will be
> 11111111 10101011
> If we then apply a bit shift n << 2; that would give us completely
> different numbers.

No. Shift operators are defined on the *values* of their operands,
not on their representations.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

 gamehack 01-05-2006 08:31 PM

Re: Bit shifts and endianness

Keith Thompson wrote:
> "gamehack" <gamehack@gmail.com> writes:
> > I was thinking today, suppose we have the number
> > n = 0xAB 0xFF

>
> That's two numbers. Do you mean 0xABFF?

Sorry, I meant 0xABFF and just separated them as distinct bytes.
>
> > which is equivalent to 44031 in decimal.

>
> Apparently so.
>
> > In big endian it will be
> > stored as
> > 10101011 11111111
> > but in little endian it will be
> > 11111111 10101011
> > If we then apply a bit shift n << 2; that would give us completely
> > different numbers.

>
> No. Shift operators are defined on the *values* of their operands,
> not on their representations.
>

Thanks
> --
> Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.

 Mark McIntyre 01-05-2006 10:51 PM

Re: Bit shifts and endianness

On Thu, 05 Jan 2006 20:26:11 GMT, in comp.lang.c , Keith Thompson
<kst-u@mib.org> wrote:

>"gamehack" <gamehack@gmail.com> writes:
>> I was thinking today, suppose we have the number

>> 10101011 11111111
>> but in little endian it will be
>> 11111111 10101011
>> If we then apply a bit shift n << 2; that would give us completely
>> different numbers.

>
>No. Shift operators are defined on the *values* of their operands,
>not on their representations.

Clarification: I assume you mean that shift operators operate on a
binary value, irrespective of how that is stored on disk or in memory.
I'm saying this because few humans would think of the value of 0xabff
as the value in binary. :-)

(and please, don't start another one of those tedious "well patently
what I said is 'right', so you're a fool" discussions again, I really
don't want to have to express my opinion of people who can't accept
that there may be more than one way to skin a cat).
Mark McIntyre
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

 Richard Heathfield 01-05-2006 11:24 PM

Re: Bit shifts and endianness

Mark McIntyre said:

> On Thu, 05 Jan 2006 20:26:11 GMT, in comp.lang.c , Keith Thompson
> <kst-u@mib.org> wrote:
>
>>No. Shift operators are defined on the *values* of their operands,
>>not on their representations.

>
> Clarification: I assume you mean that shift operators operate on a
> binary value,

No, they operate on a value. Values don't have number bases. That's merely a
representation issue.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

 slebetman@yahoo.com 01-05-2006 11:25 PM

Re: Bit shifts and endianness

Mark McIntyre wrote:
> On Thu, 05 Jan 2006 20:26:11 GMT, in comp.lang.c , Keith Thompson
> <kst-u@mib.org> wrote:
>
> >"gamehack" <gamehack@gmail.com> writes:
> >> I was thinking today, suppose we have the number

>
> >> 10101011 11111111
> >> but in little endian it will be
> >> 11111111 10101011
> >> If we then apply a bit shift n << 2; that would give us completely
> >> different numbers.

> >
> >No. Shift operators are defined on the *values* of their operands,
> >not on their representations.

>
> Clarification: I assume you mean that shift operators operate on a
> binary value, irrespective of how that is stored on disk or in memory.
> I'm saying this because few humans would think of the value of 0xabff
> as the value in binary. :-)
>

Yeah, I had this misconception before. 0xabff >> 8 always gives me
0x00ab regardless of weather the machine is big endian or little
endian. Endianness just defines weather 'ab' comes before or after 'ff'
in memory. It doesn't affect binary operations. Endianness is only an
issue when you are transferring data between machines via a file or a
network, not when you are doing the computing.

 Richard Heathfield 01-05-2006 11:29 PM

Re: Bit shifts and endianness

slebetman@yahoo.com said:

> 0xabff >> 8 always gives me 0x00ab regardless of weather

Even so, I don't recommend that you try this in the rain.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

 Keith Thompson 01-05-2006 11:51 PM

Re: Bit shifts and endianness

Mark McIntyre <markmcintyre@spamcop.net> writes:
> On Thu, 05 Jan 2006 20:26:11 GMT, in comp.lang.c , Keith Thompson
> <kst-u@mib.org> wrote:
>>"gamehack" <gamehack@gmail.com> writes:
>>> I was thinking today, suppose we have the number

>
>>> 10101011 11111111
>>> but in little endian it will be
>>> 11111111 10101011
>>> If we then apply a bit shift n << 2; that would give us completely
>>> different numbers.

>>
>>No. Shift operators are defined on the *values* of their operands,
>>not on their representations.

>
> Clarification: I assume you mean that shift operators operate on a
> binary value, irrespective of how that is stored on disk or in memory.
> I'm saying this because few humans would think of the value of 0xabff
> as the value in binary. :-)

That's one way of looking at it.

The standard describes the result of a shift operator both as "E1
(left|right)-shifted E2 bit positions" and in terms of multiplication
or division by powers of 2. I think the latter is intended to define
what the standard means by the terms "(left|right)-shifted". The
standard could as easily have defined the operators purely in terms of
multiplication or division with no reference to shifting, with the
resulting wording describing exactly the same semantics.

One advantage of describing it this way is that it avoids the OP's
question; "<<" and ">>" don't depend on endianness any more than "+"
and "-" do, and there's no potential ambiguity about the meanings of
"left" and "right".

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

 Jordan Abel 01-06-2006 12:21 AM

Re: Bit shifts and endianness

On 2006-01-05, Richard Heathfield <invalid@invalid.invalid> wrote:
> Mark McIntyre said:
>
>> On Thu, 05 Jan 2006 20:26:11 GMT, in comp.lang.c , Keith Thompson
>> <kst-u@mib.org> wrote:
>>
>>>No. Shift operators are defined on the *values* of their operands,
>>>not on their representations.

>>
>> Clarification: I assume you mean that shift operators operate on a
>> binary value,

>
> No, they operate on a value. Values don't have number bases. That's merely a
> representation issue.

The standard mentions "bits" in too many places for me to believe this.

 Chris Torek 01-06-2006 12:33 AM

Re: Bit shifts and endianness

slebetman@yahoo.com <slebetman@gmail.com> wrote:
>... Endianness is only an issue when you are transferring
>data between machines via a file or a network ...

More precisely (and more correctly :-) ), "endianness" becomes
an issue whenever someone or something takes a value apart,
so that there is a "before" and an "after", or a "left" and a
"right", or some other way of sequencing parts of the value.

For instance, imagine you are moving from one place of living to
another (e.g., moving apartments). Your car has room for 1/3 of
your bed, but not the whole thing. You take a saw to the bed and
cut it into thirds. You then transport each third to your new
location and reassemble it.

You need to reassemble it in the same order you took it apart, or
it will likely be quite uncomfortable to sleep in. :-)

Endianness in computers arises when you allow the machine to take
a value apart and ship it somewhere, and then allow some other
machine to put it together again. If the two machines use the same
order, all is well, but if they use different orders, your bed is
no longer very useful.

There are a number of ways to handle this, but the simplest is:
do the disassembly and reassembly yourself. Instead of letting
the machine do it in "machine order", do it yourself and control
the order.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603