- **C Programming**
(*http://www.velocityreviews.com/forums/f42-c-programming.html*)

- - **bitwise operator !!**
(*http://www.velocityreviews.com/forums/t647552-bitwise-operator.html*)

bitwise operator !!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 |

Re: bitwise operator !!On Dec 7, 4:21*am, chang <chinmoy.chittaran...@gmail.com> 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 |

Re: bitwise operator !!On Dec 7, 2:58*pm, s0s...@gmail.com wrote:
> On Dec 7, 4:21*am, chang <chinmoy.chittaran...@gmail.com> 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 |

Re: bitwise operator !!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 |

Re: bitwise operator !!On Dec 7, 3:44*pm, Willem <wil...@stack.nl> 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 |

Re: bitwise operator !!chang wrote:
> On Dec 7, 3:44 pm, Willem <wil...@stack.nl> 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 |

Re: bitwise operator !!On Dec 7, 4:03*pm, chang <chinmoy.chittaran...@gmail.com> wrote:
> On Dec 7, 3:44*pm, Willem <wil...@stack.nl> 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 |

Re: bitwise operator !!chang <chinmoy.chittaranjan@gmail.com> writes:
> On Dec 7, 3:44Â*pm, Willem <wil...@stack.nl> 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. |

Re: bitwise operator !!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. |

Re: bitwise operator !!"CBFalconer" <cbfalconer@yahoo.com> wrote in message
news:493C6874.A5B35C75@yahoo.com... > 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 |

All times are GMT. The time now is 06:22 AM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.