Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #15809 > unrolled thread
| Started by | ishwar mehta <imehta285@gmail.com> |
|---|---|
| First post | 2012-01-15 01:54 -0800 |
| Last post | 2012-01-17 03:43 -0800 |
| Articles | 9 — 8 participants |
Back to article view | Back to comp.lang.c
RELATION BETWEEN OPERATORS ishwar mehta <imehta285@gmail.com> - 2012-01-15 01:54 -0800
Re: RELATION BETWEEN OPERATORS Dr Nick <3-nospam@temporary-address.org.uk> - 2012-01-15 10:21 +0000
Re: RELATION BETWEEN OPERATORS "BartC" <bc@freeuk.com> - 2012-01-15 10:51 +0000
Re: RELATION BETWEEN OPERATORS Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-15 13:42 +0000
Re: RELATION BETWEEN OPERATORS Peter Nilsson <airia@acay.com.au> - 2012-01-17 18:19 -0800
Re: RELATION BETWEEN OPERATORS Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-15 04:01 -0800
RELATION BETWEEN OPERATORS John Bode <jfbode1029@gmail.com> - 2012-01-16 09:11 -0800
Re: RELATION BETWEEN OPERATORS Keith Thompson <kst-u@mib.org> - 2012-01-16 12:03 -0800
Re: RELATION BETWEEN OPERATORS John Bode <jfbode1029@gmail.com> - 2012-01-17 03:43 -0800
| From | ishwar mehta <imehta285@gmail.com> |
|---|---|
| Date | 2012-01-15 01:54 -0800 |
| Subject | RELATION BETWEEN OPERATORS |
| Message-ID | <a1a4c454-f03c-4fbe-a936-f671b1de8433@v6g2000pba.googlegroups.com> |
HI FRIENDS,SOMEWHERE i have Seen that
Expressions (x%8) and (x&7) in C , are equivalent.. that is
if i write a program like
int main ()
{
int x=50;
printf("%d\n", x%8);
printf("%d\n", x&7);
return 0 ;
}
output:
2
2
SO Frnd,plz help me to figure out ,what is the exact relation betwwen
the operators % and & .
plzzzzzzz.......
[toc] | [next] | [standalone]
| From | Dr Nick <3-nospam@temporary-address.org.uk> |
|---|---|
| Date | 2012-01-15 10:21 +0000 |
| Message-ID | <87ipkdp8oz.fsf@temporary-address.org.uk> |
| In reply to | #15809 |
ishwar mehta <imehta285@gmail.com> writes:
> HI FRIENDS,SOMEWHERE i have Seen that
In your homework assignment?
> Expressions (x%8) and (x&7) in C , are equivalent.. that is
They aren't.
> if i write a program like
>
>
> int main ()
> {
> int x=50;
try it with x=-50 and you'll see the difference.
> printf("%d\n", x%8);
> printf("%d\n", x&7);
> return 0 ;
> }
> output:
> 2
> 2
>
> SO Frnd,plz help me to figure out ,what is the exact relation betwwen
> the operators % and & .
What does the % operator do?
What does the & operator do?
Why will they give the same results for some range of values (NB, this
is maths rather than C).
> plzzzzzzz.......
Can you take that bee out of here please, it's driving me nuts.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2012-01-15 10:51 +0000 |
| Message-ID | <jeub6q$khf$1@dont-email.me> |
| In reply to | #15809 |
"ishwar mehta" <imehta285@gmail.com> wrote in message
news:a1a4c454-f03c-4fbe-a936-f671b1de8433@v6g2000pba.googlegroups.com...
> HI FRIENDS,SOMEWHERE i have Seen that
>
> Expressions (x%8) and (x&7) in C , are equivalent.. that is
> if i write a program like
>
>
> int main ()
> {
> int x=50;
> printf("%d\n", x%8);
> printf("%d\n", x&7);
> return 0 ;
> }
> output:
> 2
> 2
>
> SO Frnd,plz help me to figure out ,what is the exact relation betwwen
> the operators % and & .
The results of A%N and A&(N-1) will generally be different.
But if N is a power of 2 as in your example, eg 2**M, then it's not
difficult to see why they might be expected to be the same (both isolate the
last M bits of A).
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-01-15 13:42 +0000 |
| Message-ID | <0.c214f1af9235c08b85dc.20120115134252GMT.87k44tozdv.fsf@bsb.me.uk> |
| In reply to | #15811 |
"BartC" <bc@freeuk.com> writes:
> "ishwar mehta" <imehta285@gmail.com> wrote in message
> news:a1a4c454-f03c-4fbe-a936-f671b1de8433@v6g2000pba.googlegroups.com...
>> HI FRIENDS,SOMEWHERE i have Seen that
>>
>> Expressions (x%8) and (x&7) in C , are equivalent.. that is
>> if i write a program like
>>
>>
>> int main ()
>> {
>> int x=50;
>> printf("%d\n", x%8);
>> printf("%d\n", x&7);
>> return 0 ;
>> }
>> output:
>> 2
>> 2
>>
>> SO Frnd,plz help me to figure out ,what is the exact relation betwwen
>> the operators % and & .
>
> The results of A%N and A&(N-1) will generally be different.
>
> But if N is a power of 2 as in your example, eg 2**M, then it's not
> difficult to see why they might be expected to be the same (both
> isolate the last M bits of A).
But they don't both do that. % is defined (in C99) as an essentially
arithmetic operation, while & is defined in terms of the representation.
This is most clearly seen when A is negative. -2 % 8 is -2 no matter
what representation is used for negative numbers, but -2 & 7 is either
6, 5 or 2 depending on which of the three representations for negative
numbers is in use.
I know you were thinking of positive N, but I think it confuses the
issue to look at the cases when they *are* the same. The relationship
between things that have some similarity is almost always made most
clear by pointing out the differences.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Peter Nilsson <airia@acay.com.au> |
|---|---|
| Date | 2012-01-17 18:19 -0800 |
| Message-ID | <a9e871c8-a7bc-429f-849a-c11ec5bd9d2b@x4g2000pbq.googlegroups.com> |
| In reply to | #15814 |
Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> "BartC" <b...@freeuk.com> writes:
> > "ishwar mehta" <imehta...@gmail.com> wrote
> >> ...i have Seen that
> >> Expressions (x%8) and (x&7) in C , are equivalent..
If x is an unsigned type, or signed with a non-negative value, yes.
> >> that is if i write a program like
> >>
> >> int main ()
> >> {
> >> int x=50;
> >> printf("%d\n", x%8);
> >> printf("%d\n", x&7);
> >> return 0 ;
> >> }
> >> output:
> >> 2
> >> 2
> >>
> >> SO Frnd,plz help me to figure out ,what is the exact relation
> >> betwwen the operators % and & .
The % operator yields 'what remains' from division; the & operator
yields
'what remains' if you exclude certain value bits.
> > The results of A%N and A&(N-1) will generally be different.
> >
> > But if N is a power of 2 as in your example, eg 2**M, then it's not
> > difficult to see why they might be expected to be the same (both
> > isolate the last M bits of A).
>
> But they don't both do that. % is defined (in C99) as an essentially
> arithmetic operation,
No less in C90.
> while & is defined in terms of the representation.
Yes and no. It operates on value and sign bits, but not padding bits.
One can think of x % 1000 as a decimal-wise masking of value places,
so
there is a relation between &1, &3, &7, ... and %10, %100, %1000...
> This is most clearly seen when A is negative.
In the general case, C programmers shouldn't be using bitwise
operators
on signed types. If we limited the discussion to unsigned types, then
in that (ideal) environment, your point is of no consequence.
Under C90, division needn't round towards zero, so even for %, there
is a case to limit use to non-negative integers for consistent
results.
Of course, the real question is when to use &7 and when to use %8.
The answer to that depends on context. If you want the remainder when
dividing by 8, then use %. If you want the lower 3 bits, then use &.
Whilst division can be expensive on some CPUs, most modern compilers
are more than good enough to optimise %8 without programmer
assistance.
If you see &7 used in place of %8, then you're just witnessing someone
using something that was clever and critcal 40+ years ago, not
something
that's clever or critical now. Or, if there's a case where the
compiler
isn't optimising %8 to &7, then it's more likely the code isn't using
the most practical type for the operation at hand (e.g. using int
rather
than size_t.)
--
Peter
[toc] | [prev] | [next] | [standalone]
| From | Nick Keighley <nick_keighley_nospam@hotmail.com> |
|---|---|
| Date | 2012-01-15 04:01 -0800 |
| Message-ID | <4dcf892b-baa4-4a60-889e-f3228a7c2cc5@t13g2000yqg.googlegroups.com> |
| In reply to | #15809 |
On Jan 15, 9:54 am, ishwar mehta <imehta...@gmail.com> wrote:
> HI FRIENDS,SOMEWHERE i have Seen that
>
> Expressions (x%8) and (x&7) in C , are equivalent.. that is
> if i write a program like
>
> int main ()
> {
> int x=50;
> printf("%d\n", x%8);
> printf("%d\n", x&7);
> return 0 ;}
>
> output:
> 2
> 2
try it with more numbers
> SO Frnd,plz help me to figure out ,what is the exact relation betwwen
> the operators % and & .
learn binary
[toc] | [prev] | [next] | [standalone]
| From | John Bode <jfbode1029@gmail.com> |
|---|---|
| Date | 2012-01-16 09:11 -0800 |
| Message-ID | <18488243.1382.1326733887573.JavaMail.geo-discussion-forums@yqni15> |
| In reply to | #15809 |
There is no relationship between the two. One's an arithmetic operation, the other's a bitwise operation. For some combinations of operands they will produce the same results, but that's by coincidence, not design.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-01-16 12:03 -0800 |
| Message-ID | <ln8vl71kk8.fsf@nuthaus.mib.org> |
| In reply to | #15850 |
John Bode <jfbode1029@gmail.com> writes:
> There is no relationship between the two. One's an arithmetic
> operation, the other's a bitwise operation. For some combinations of
> operands they will produce the same results, but that's by
> coincidence, not design.
The question was about the relationship between (x%8) and (x&7), but you
can't tell that by reading your answer. Please quote enough context
from the parent article so your answer makes sense by itself. (And
don't delete the "Re: " from the subject header.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | John Bode <jfbode1029@gmail.com> |
|---|---|
| Date | 2012-01-17 03:43 -0800 |
| Message-ID | <98a101f5-2dde-433a-9f14-3d46dc7af5c0@1g2000yqv.googlegroups.com> |
| In reply to | #15853 |
On Jan 16, 2:03 pm, Keith Thompson <ks...@mib.org> wrote: > John Bode <jfbode1...@gmail.com> writes: > > There is no relationship between the two. One's an arithmetic > > operation, the other's a bitwise operation. For some combinations of > > operands they will produce the same results, but that's by > > coincidence, not design. > > The question was about the relationship between (x%8) and (x&7), but you > can't tell that by reading your answer. Please quote enough context > from the parent article so your answer makes sense by itself. (And > don't delete the "Re: " from the subject header.) > > -- > Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst> > Will write code for food. > "We must do something. This is something. Therefore, we must do this." > -- Antony Jay and Jonathan Lynn, "Yes Minister" Blame Google Groups' mobile interface; I *thought* it was quoting context, but it turned out it wasn't. As lame as GG can be, it's even lamer on Android.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web