Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > bitwise operator !!

Reply
Thread Tools

bitwise operator !!

 
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      12-08-2008
On 8 Dec, 08:03, Ian Collins <(E-Mail Removed)> wrote:
> Flash Gordon wrote:
>
> > It can go wrong with 2's complement machines as well. There have been
> > processors with only a logical ****.

>
>


I'm guessing you had to use multiply or divide if you actually needed
to
shift bits
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-08-2008
"Jujitsu Lizard" <(E-Mail Removed)> writes:
[...]
> The other posters in this newsgroup are knuckleheads who don't
> understand C, computing in general, or digital logic design. Let me
> help you out here.

[...]

Perhaps that was meant to be a joke, but considering the later posts
in this thread, perhaps you should reconsider making statements like
that.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
jameskuyper
Guest
Posts: n/a
 
      12-08-2008
Jujitsu Lizard wrote:
....
> I did not realize that sign-magnitude representation was legal on a machine
> with a C compiler (why would any sane person represent integers that way?).


I can't vouch for this from my own personal knowledge, but on
2007-11-28, Doug Gwyn asserted on comp.std.c that

| There are some sign-magnitude DSPs, for example, and at least one
mainframe family (Unisys A) is still in
| production and worthy of being able to support a standard-conformant
C compiler.

I similarly can't vouch for the sanity of the people who designed such
systems, nor do I have any idea what reasons they might give to
justify that choice. However, I'd recommend not suggesting that
they're insane until after you've discovered what those reasons might
be.

As a practical matter, the question is not whether those machine
designers were sane, nor whether their choice was a good one. What
matters was such machines have existed in the past, still do exist,
and are still in use by a fair number of people (which implies that
the choice was probably NOT completely insane). It is inconsistent
with the goals of the C standard to arbitrarily prevent the creation
of conforming implementations of C on such machines.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-08-2008
pete <(E-Mail Removed)> writes:

> Jujitsu Lizard wrote:
>> "blargg" <(E-Mail Removed)> wrote in message
>> news:blargg.h4g-0712081920540001@192.168.1.4...
>>> "Jujitsu Lizard" wrote:
>>> [...]
>>>> As far as the right-shift operator applied to negative numbers,
>>>> you'll find
>>>> that every compiler known to man either propagates the sign bit into the
>>>> leftmost position or else uses 0's (depending on the machine
>>>> instructions
>>>> chosen). Usually the only unknown is the "fill".
>>>>
>>>> You can get around this if you really need to.
>>>>
>>>> Need to shift a signed number right 5 bits?
>>>>
>>>> if (x<0)
>>>> {
>>>> result = (x >> 5) | (-1 << (sizeof(int) * 8 - 5);
>>>> }
>>>> else
>>>> {
>>>> result = ((unsigned)(x)) >> 5;
>>>> }
>>>
>>> The above code seems questionable (assumes char is 8 bits, two's
>>> complement where it ORs -1 shifted?, and the unsigned cast at the end is
>>> unnecessary). How about this? (the conversion to unsigned before negation
>>> is to properly handle the most negative value of a signed int on a two's
>>> complement machine)
>>>
>>> (x < 0) ? -(int) (-(unsigned) x >> 5) : (x >> 5)

>>
>> My intent was to bypass the ambiguous handling of the sign
>> propagation on right shift by masking in bits as if the sign bit had
>> propagated. In other words, the code I posted was designed to
>> generate code that would effectively always propagate the sign bit
>> on right shift.

>
> if (0 > x) {
> x = -x;


This can overflow. I would avoid all bit operations on signed types.

> x >>= 5;
> x = -x;
> }


--
Ben.
 
Reply With Quote
 
jameskuyper
Guest
Posts: n/a
 
      12-08-2008
Jujitsu Lizard wrote:
....
> I did not realize that sign-magnitude representation was legal on a machine
> with a C compiler (why would any sane person represent integers that way?).


The most recent message from Ben Bacarisse reminds me of one advantage
of the typical sign-magnitude representation: INT_MIN is the same as
the mathematical value of -INT_MAX, which means that the unary '-'
operator always has standard-defined behavior on such implementations.
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      12-08-2008
"Jujitsu Lizard" <(E-Mail Removed)> writes:
>>> The other posters in this newsgroup are knuckleheads who don't
>>> understand C, computing in general, or digital logic design.


> I thought that shifting a signed integer to the left was perfectly
> defined but shifting a signed integer to the right had the bothersome
> complexity.
>
> Am I wrong?


At least as wrong as any other knucklehead on the group.

However, the majority of the poeple you're disparaging with
your earlier comment appear to understand C vastly better
than you do.

At least you had the humility to ask if you were wrong when
you displayed your utter, almost painful, ignorance of the
language. Maybe there's hope for you.

But I won't hold my breath.

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-08-2008
blargg wrote:
>

.... snip ...
>
> BTW, in another message in this thread someone said that shifting
> a signed value right is undefined. My copy of the C99 standard
> says otherwise:
>
>> The result of E1 >> E2 is E1 right-shifted E2 bit positions. If
>> E1 has an unsigned type or if E1 has a signed type and a
>> nonnegative value, the value of the result is the integral part
>> of the quotient of E1 / 2^E2. If E1 has a signed type and a
>> negative value, the resulting value is implementation-defined.


Your quote (repeated above) specifies implementation-defined. This
also means that you don't know what your code will do on another
compiler or system.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-08-2008
blargg wrote:
> jameskuyper wrote:
>> Jujitsu Lizard wrote:
>> ...
>>> I did not realize that sign-magnitude representation was legal
>>> on a machine with a C compiler (why would any sane person
>>> represent integers that way?).

>>
>> The most recent message from Ben Bacarisse reminds me of one
>> advantage of the typical sign-magnitude representation: INT_MIN
>> is the same as the mathematical value of -INT_MAX, which means
>> that the unary '-' operator always has standard-defined behavior
>> on such implementations.

>
> Sorry if I'm repeating myself from another message (or forgot the
> reason given), but couldn't a compiler on a two's complement
> platform do the same thing, define INT_MIN to be -INT_MAX? For
> example, if int is 16 bits on such a machine, then INT_MAX would
> be 32767 and INT_MIN would be -32767. Then you're guaranteed that
> every valid value of an int can be negated properly.


And then, on such a machine, you have an illegal bit pattern for an
integer left over. No other languages or programs on that machine
understand this. What are you going to do with it?

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-08-2008
pete wrote:
> Jujitsu Lizard wrote:
>

.... snip ...
>
>> My intent was to bypass the ambiguous handling of the sign
>> propagation on right shift by masking in bits as if the sign bit
>> had propagated. In other words, the code I posted was designed
>> to generate code that would effectively always propagate the
>> sign bit on right shift.

>
> if (0 > x) {
> x = -x;
> x >>= 5;
> x = -x;
> }


Fatal. If x == INT_MIN on entry to that, you can have undefined
behavior in the x = -x statement. However the simple, and
understandable, x = x / 32 will work.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
lawrence.jones@siemens.com
Guest
Posts: n/a
 
      12-08-2008
Jujitsu Lizard <(E-Mail Removed)> wrote:
>
> I did not realize that sign-magnitude representation was legal on a machine
> with a C compiler (why would any sane person represent integers that way?).


Why are floating-point numbers usually represented that way? There are
machines that use the same format for both.
--
Larry Jones

That's one of the remarkable things about life. It's never so
bad that it can't get worse. -- Calvin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Equivalence in use of bitwise | operator and + operator Ioannis Vranos C++ 8 11-14-2008 11:03 PM
How to calculate size of an int without using the sizeof operator but using bitwise operator Manish_Ganvir C Programming 13 02-14-2005 07:24 PM
Bitwise operator question... littlehobo C++ 10 10-01-2003 02:40 PM
Re: bitwise operator on a struct Peter Shaggy Haywood C Programming 0 06-24-2003 11:59 PM
Re: bitwise operator on a struct Peter Shaggy Haywood C Programming 0 06-24-2003 11:59 PM



Advertisments