Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

bitwise operator !!

 
 
chang
Guest
Posts: n/a
 
      12-07-2008
Hi .
I am surprised about this C Question answer even i am also not sharp
in C.

Programme is -:

#include<stdio.h>
void main()
{
int a= -3;

printf("%d",a>>1);
}

Here the printf value is coming :-2.
How come here the value is coming is -2. Please help me to know what
is happenning for negative value(a= -3).

Thanks for advance

Chang
 
Reply With Quote
 
 
 
 
s0suk3@gmail.com
Guest
Posts: n/a
 
      12-07-2008
On Dec 7, 4:21*am, chang <(E-Mail Removed)> wrote:
> Hi .
> *I am surprised about this C Question answer even i am also not sharp
> in C.
>
> Programme is -:
>
> #include<stdio.h>
> void main()
> {
> * * * * int a= -3;
>
> * * * * printf("%d",a>>1);
>
> }
>
> Here the printf value is coming :-2.
> How come here the value is coming is -2. Please help me to know what
> is happenning for negative value(a= -3).


It depends on how your machine stores negative numbers. If your
machine is like mine, the 32-bit integer -3 will look like this in
binary:

11111111111111111111111111111101

Then you perform a right shift on this integer. Shifting right a
negative (signed) integer is generally a bad idea because it's
implementation-defined whether 0's or 1's are added to the left. If
your machine is like mine, this right shift will add a 1 bit to the
left, and the resulting integer will look like this:

11111111111111111111111111111110

And that is -2 in decimal.

Sebastian

 
Reply With Quote
 
 
 
 
chang
Guest
Posts: n/a
 
      12-07-2008
On Dec 7, 2:58*pm, (E-Mail Removed) wrote:
> On Dec 7, 4:21*am, chang <(E-Mail Removed)> wrote:
>
>
>
>
>
> > Hi .
> > *I am surprised about this C Question answer even i am also not sharp
> > in C.

>
> > Programme is -:

>
> > #include<stdio.h>
> > void main()
> > {
> > * * * * int a= -3;

>
> > * * * * printf("%d",a>>1);

>
> > }

>
> > Here the printf value is coming :-2.
> > How come here the value is coming is -2. Please help me to know what
> > is happenning for negative value(a= -3).

>
> It depends on how your machine stores negative numbers. If your
> machine is like mine, the 32-bit integer -3 will look like this in
> binary:
>
> 11111111111111111111111111111101
>
> Then you perform a right shift on this integer. Shifting right a
> negative (signed) integer is generally a bad idea because it's
> implementation-defined whether 0's or 1's are added to the left. If
> your machine is like mine, this right shift will add a 1 bit to the
> left, and the resulting integer will look like this:
>
> 11111111111111111111111111111110
>
> And that is -2 in decimal.
>
> Sebastian- Hide quoted text -
>
> - Show quoted text -



hi
Thanks all.

I got this idea but why this kind of system is there: -1
representating in this format 11111111111111111111111111111111?
Actually what is defination lies in this?

Thanks
Chang
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      12-07-2008
chang wrote:
) I got this idea but why this kind of system is there: -1
) representating in this format 11111111111111111111111111111111?
) Actually what is defination lies in this?

Simple. Look at this calculation of zero minus one, in binary.
(I use 4 bits to keep it simple)

0000
0001 -
----
1 (carry the one)

0000
0001 -
1 (this is the carry)
----
11 (carry the one again)

0000
0001 -
1 (this is the carry)
----
111 (carry the one again)

0000
0001 -
1 (this is the carry)
----
1111 (carry the one again)

0000
0001 -
1 (this carry can't be used, it's a so-called carry overflow)
----
1111

So, the result of zero minus one, in binary, is a row of '1' bits.

(From this, it should be easy to determine what -2 is in binary.)


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
chang
Guest
Posts: n/a
 
      12-07-2008
On Dec 7, 3:44*pm, Willem <(E-Mail Removed)> wrote:
> chang wrote:
>
> ) I got this idea but why this kind of system is there: -1
> ) representating in this format 11111111111111111111111111111111?
> ) Actually what is defination lies in this?
>
> Simple. *Look at this calculation of zero minus one, in binary.
> (I use 4 bits to keep it simple)
>
> * 0000
> * 0001 -
> * ----
> * * *1 (carry the one)
>
> * 0000
> * 0001 -
> * * 1 *(this is the carry)
> * ----
> * * 11 (carry the one again)
>
> * 0000
> * 0001 -
> * *1 *(this is the carry)
> * ----
> * *111 (carry the one again)
>
> * 0000
> * 0001 -
> * 1 *(this is the carry)
> * ----
> * 1111 (carry the one again)
>
> * 0000
> * 0001 -
> *1 * * (this carry can't be used, it's a so-called carry overflow)
> * ----
> * 1111
>
> So, the result of zero minus one, in binary, is a row of '1' bits.
>
> (From this, it should be easy to determine what -2 is in binary.)
>
> SaSW, Willem
> --
> Disclaimer: I am in no way responsible for any of the statements
> * * * * * * made in the above text. For all I know I might be
> * * * * * * drugged or something..
> * * * * * * No I'm not paranoid. You all think I'm paranoid, don't you !
> #EOT



Thanks .

But i am actually looking for why (-1) is represented as 0XFFFFFFFF or
in binary all bits will be 1!!!


Please guide me to know about this.

Chang
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      12-07-2008
chang wrote:
> On Dec 7, 3:44 pm, Willem <(E-Mail Removed)> wrote:
>> chang wrote:
>>
>> ) I got this idea but why this kind of system is there: -1
>> ) representating in this format 11111111111111111111111111111111?
>> ) Actually what is defination lies in this?
>>
>> Simple. Look at this calculation of zero minus one, in binary.
>> (I use 4 bits to keep it simple)
>>
>> 0000
>> 0001 -
>> ----
>> 1 (carry the one)

.....
>> 0000
>> 0001 -
>> 1 (this carry can't be used, it's a so-called carry overflow)
>> ----
>> 1111
>>
>> So, the result of zero minus one, in binary, is a row of '1' bits.
>>
>> (From this, it should be easy to determine what -2 is in binary.)
>>
>> SaSW, Willem
>> --
>> Disclaimer: I am in no way responsible for any of the statements
>> made in the above text. For all I know I might be
>> drugged or something..
>> No I'm not paranoid. You all think I'm paranoid, don't you !
>> #EOT

>
>
> Thanks .
>
> But i am actually looking for why (-1) is represented as 0XFFFFFFFF or
> in binary all bits will be 1!!!


Didn't Willem just explain exactly that?

Try adding binary 000...001 to your row of 1 bits. You should get all zeros.
The same as adding 1 to -1. So -1 and all 1s are equivalent in that
encoding.

The same sometimes occurs in decimal. You might remember the old-fashioned
tape counters, which when you rewound would show 002, 001, 000, 999, ...
where you might have expected 002, 001, 000, -001,... But adding 001 to 999
gives 000, so it must be right!

--
Bartc

 
Reply With Quote
 
Sri Harsha Dandibhotla
Guest
Posts: n/a
 
      12-07-2008
On Dec 7, 4:03*pm, chang <(E-Mail Removed)> wrote:
> On Dec 7, 3:44*pm, Willem <(E-Mail Removed)> wrote:
>
>
>
> > chang wrote:

>
> > ) I got this idea but why this kind of system is there: -1
> > ) representating in this format 11111111111111111111111111111111?
> > ) Actually what is defination lies in this?

>
> > Simple. *Look at this calculation of zero minus one, in binary.
> > (I use 4 bits to keep it simple)

>
> > * 0000
> > * 0001 -
> > * ----
> > * * *1 (carry the one)

>
> > * 0000
> > * 0001 -
> > * * 1 *(this is the carry)
> > * ----
> > * * 11 (carry the one again)

>
> > * 0000
> > * 0001 -
> > * *1 *(this is the carry)
> > * ----
> > * *111 (carry the one again)

>
> > * 0000
> > * 0001 -
> > * 1 *(this is the carry)
> > * ----
> > * 1111 (carry the one again)

>
> > * 0000
> > * 0001 -
> > *1 * * (this carry can't be used, it's a so-called carry overflow)
> > * ----
> > * 1111

>
> > So, the result of zero minus one, in binary, is a row of '1' bits.

>
> > (From this, it should be easy to determine what -2 is in binary.)

>
> > SaSW, Willem
> > --
> > Disclaimer: I am in no way responsible for any of the statements
> > * * * * * * made in the above text. For all I know I might be
> > * * * * * * drugged or something..
> > * * * * * * No I'm not paranoid. You all think I'm paranoid, don't you !
> > #EOT

>
> Thanks .
>
> But i am actually looking for why (-1) is represented as 0XFFFFFFFF or
> in binary all bits will be *1!!!
>
> Please guide me to know about this.
>
> Chang


http://en.wikipedia.org/wiki/Two%27s_complement
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-07-2008
chang <(E-Mail Removed)> writes:

> On Dec 7, 3:44*pm, Willem <(E-Mail Removed)> wrote:
>> chang wrote:
>>
>> ) I got this idea but why this kind of system is there: -1
>> ) representating in this format 11111111111111111111111111111111?
>> ) Actually what is defination lies in this?
>>
>> Simple. *Look at this calculation of zero minus one, in binary.
>> (I use 4 bits to keep it simple)
>>
>> * 0000
>> * 0001 -
>> * ----
>> * * *1 (carry the one)
>>
>> * 0000
>> * 0001 -
>> * * 1 *(this is the carry)
>> * ----
>> * * 11 (carry the one again)
>>
>> * 0000
>> * 0001 -
>> * *1 *(this is the carry)
>> * ----
>> * *111 (carry the one again)
>>
>> * 0000
>> * 0001 -
>> * 1 *(this is the carry)
>> * ----
>> * 1111 (carry the one again)
>>
>> * 0000
>> * 0001 -
>> *1 * * (this carry can't be used, it's a so-called carry overflow)
>> * ----
>> * 1111
>>
>> So, the result of zero minus one, in binary, is a row of '1' bits.
>>
>> (From this, it should be easy to determine what -2 is in binary.)
>>
>> SaSW, Willem
>> --


Best not to quote sigs (the part after the "-- ").

> Thanks .
>
> But i am actually looking for why (-1) is represented as 0XFFFFFFFF or
> in binary all bits will be 1!!!


That is what Willem was doing. Let me try another way (the is related
to his example). We want -1 + 1 == 0, yes? What happens to "all
ones" when we add one?

--
Ben.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-08-2008
chang wrote:
>
> I am surprised about this C Question answer even i am also not
> sharp in C.
>
> #include<stdio.h>
> void main() {
> int a= -3;
> printf("%d",a>>1);
> }
>
> Here the printf value is coming :-2.
> How come here the value is coming is -2. Please help me to know
> what is happenning for negative value(a= -3).


Attempting to shift a negative value results in either
implementation or undefined behaviour. Don't ever apply the shift
operator to a negative value. You may use division, however.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      12-08-2008
"CBFalconer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> chang wrote:
>>
>> I am surprised about this C Question answer even i am also not
>> sharp in C.
>>
>> #include<stdio.h>
>> void main() {
>> int a= -3;
>> printf("%d",a>>1);
>> }
>>
>> Here the printf value is coming :-2.
>> How come here the value is coming is -2. Please help me to know
>> what is happenning for negative value(a= -3).

>
> Attempting to shift a negative value results in either
> implementation or undefined behaviour. Don't ever apply the shift
> operator to a negative value. You may use division, however.


Apparently division of negative values is also implementation defined (in
C90).

-3/2 can be either -2 or -1.

--
Bartc

 
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