freelanceprogrammers.org Forum Index » C

Re: Digest Number


View user's profile Post To page top
cdersi Posted: Sat Dec 18, 2004 2:59 pm


Joined: 18 Dec 2004

Posts: 1
Re: Digest Number
<< is bitwise left shift operator.
The result of the expression a << b
is a value that the value a is shifted right by b positions (bits).
(b number of bits removed from the left and b number of 0 bits added from right)
the value b should be less than the number of total bits  of a
 
 

>> is bitwise right shift operator.
The result of the expression a >> b
is a value that the value a is shifted left by b positions (bits).

(b number of bits removed from the right and b number of 0 bits added from left (if a is unsigned or if a is a signed value greater than 0)


the value b should be less than the number of total bits  of a
suppose that the integer 5 is represented as follows
 
0000 0000 0000 0101
 
5 >> 1 will be the value
 

0000 0000 0000 0010  (2)
 

5 >> 2 will be the value
 

0000 0000 0000 0001  (1)
 
5 << 1 will be the value
 

0000 0000 0000 1010  (10)
 

5 << 2 will be the value
 

0000 0000 0001 0100  (20)
 
in your main function, you call the function printf to write the value of integer expressions to stdout in decimal system,
I mean the expresions x, x << 2 and x >> 2.
 
nerg
 
 
 

----- Original Message -----
From: suresh kanna
To: Programmers-Town@yahoogroups.com
Sent: Thursday, December 16, 2004 3:50 PM
Subject: (PT) Digest Number

hello sir/mam,
can anyone can explain me how this program is solved manually and in compiler....
main()
{
int x=5;
printf("%d %d %d ",x,x<<2,x>>2);
}
The answer for this is  5,1,20... but how the answer is approached...
can anyone helpme.....
reply in suresh_hearts@...
 
thz,
with warm wishes bye,
suresh
 
 
 


Do you Yahoo!?Jazz up your holiday email with celebrity designs. Learn more. To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Reply with quote
Send private message
View user's profile Post To page top
ashwin_mittal26 Posted: Tue Dec 21, 2004 9:52 am


Joined: 27 Dec 2004

Posts: 14
Re: Digest Number
--- Shyan Lam <sflam@...> wrote:

> Reply embedded...
>
> > From: suresh kanna
> [mailto:suresh_hearts@...]
> > Sent: Thursday, December 16, 2004 7:51 AM
> > To: Programmers-Town@yahoogroups.com
> > Subject: (PT) Digest Number
> >
> > hello sir/mam,
> > can anyone can explain me how this program is
> solved manually and
> > in compiler....
> > main()
> > {
> > int x=5;
> > printf("%d %d %d ",x,x<<2,x>>2);
> > }
> > The answer for this is  5,1,20... but how the
> answer is approached...
> > can anyone helpme.....
>
> Do you understand what does the `<<` and `>>`
> operators do? What does your
> text said?
>
> They are called bit-shift operators.
>
>
> Bit 7 6 5 4 3 2 1 0
> +===============================+
> 5 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
> +===============================+
> | | |
> +-------+ | |
> | +-------+ |
> | | +-------+
> After << 2: v v v
> Bit 7 6 5 4 3 2 1 0
> +===================================+
> 20 | 0 | 0 | 0 | 1 | 0 | 1 | (0) | (0) |
> +===================================+
> ^^^^^^^^^^^ Two `0`
> pushed
> from
> right
>
>
> Bit 7 6 5 4 3 2 1 0
> +===============================+
> 5 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
> +===============================+
> | | |
> | | +-----------+
> | +-----------+ |
> +-----------+ | |
> After >> 2: v | |
> Bit 7 6 5 4 3 2 1 0 v v
> +===================================+ - - - +
> 1 | (0) | (0) | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
> +===================================+ - - - +
> ^^^^^^^^^^^^ ^^^^^^^^^
> Two `0` pushed Two bits
> pushed
> From left out
>
>
>
> In case your book did not mention it, the shift
> operator should be used on
> unsigned value.
>
> I suggest you read what the standard said when using
> the bitwise shift
> operator on signed value:
> <http://dev.unicals.com/c99-draft.html#6.5.7>
>
>
> > reply in suresh_hearts@...
>
> All reply should be sent to the group, for others to
> comments and reviews
> for correctness. All members should benefit from
> the discussions, not just
> you.
>
> HTH
> Shyan
>
>
Hi Shyam,

Can you please explain this the same procedure when
int x = -5;

Regards,
Ashwin Mittal.



__________________________________
Do you Yahoo!?
Send holiday email and support a worthy cause. Do good.
http://celebrity.mail.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
mummy_return... Posted: Tue Dec 21, 2004 11:39 am


Joined: 24 Dec 2004

Posts: 3
Re: Digest Number
--- ashwin mittal <ashwin_mittal26@...> wrote:

>
>
>
> --- Shyan Lam <sflam@...> wrote:
>
> > Reply embedded...
> >
> > > From: suresh kanna
> > [mailto:suresh_hearts@...]
> > > Sent: Thursday, December 16, 2004 7:51 AM
> > > To: Programmers-Town@yahoogroups.com
> > > Subject: (PT) Digest Number
> > >
> > > hello sir/mam,
> > > can anyone can explain me how this program is
> > solved manually and
> > > in compiler....
> > > main()
> > > {
> > > int x=5;
> > > printf("%d %d %d ",x,x<<2,x>>2);
> > > }
> > > The answer for this is  5,1,20... but how the
> > answer is approached...
> > > can anyone helpme.....
> >
> > Do you understand what does the `<<` and `>>`
> > operators do? What does your
> > text said?
> >
> > They are called bit-shift operators.
> >
> >
> > Bit 7 6 5 4 3 2 1 0
> > +===============================+
> > 5 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
> > +===============================+
> > | | |
> > +-------+ | |
> > | +-------+ |
> > | | +-------+
> > After << 2: v v v
> > Bit 7 6 5 4 3 2 1 0
> > +===================================+
> > 20 | 0 | 0 | 0 | 1 | 0 | 1 | (0) | (0) |
> > +===================================+
> > ^^^^^^^^^^^ Two `0`
> > pushed
> > from
> > right
> >
> >
> > Bit 7 6 5 4 3 2 1 0
> > +===============================+
> > 5 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
> > +===============================+
> > | | |
> > | | +-----------+
> > | +-----------+ |
> > +-----------+ | |
> > After >> 2: v | |
> > Bit 7 6 5 4 3 2 1 0 v v
> > +===================================+ - - -
> +
> > 1 | (0) | (0) | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1
> |
> > +===================================+ - - -
> +
> > ^^^^^^^^^^^^
> ^^^^^^^^^
> > Two `0` pushed Two
> bits
> > pushed
> > From left out
> >
> >
> >
> > In case your book did not mention it, the shift
> > operator should be used on
> > unsigned value.
> >
> > I suggest you read what the standard said when
> using
> > the bitwise shift
> > operator on signed value:
> > <http://dev.unicals.com/c99-draft.html#6.5.7>
> >
> >
> > > reply in suresh_hearts@...
> >
> > All reply should be sent to the group, for others
> to
> > comments and reviews
> > for correctness. All members should benefit from
> > the discussions, not just
> > you.
> >
> > HTH
> > Shyan
> >
> >
> Hi Shyam,
>
> Can you please explain this the same procedure when
> int x = -5;
>
> Regards,
> Ashwin Mittal.
>
>
>
> __________________________________
> Do you Yahoo!?
> Send holiday email and support a worthy cause. Do
> good.
> http://celebrity.mail.yahoo.com
>
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor
> --------------------~-->
> $4.98 domain names from Yahoo!. Register anything.
>
http://us.click.yahoo.com/Q7_YsB/neXJAA/yQLSAA/EbFolB/TM
>
--------------------------------------------------------------------~->
>
>
> To unsubscribe :
> programmers-town-unsubscribe@yahoogroups.com
>
>
> Yahoo! Groups Links
>
>
> Programmers-Town-unsubscribe@yahoogroups.com
>
>
>


hi frnds,

u will be getting the ans 5 , 20 and 1 . but the ans
given is in the format of 5,1 and 20.

plz clear my doubt .

i have one more doubt , from which side does the C
starts processing the data.i.e
suppose this is the statement given to u.
printf("%d %d %d ",x,x<<2,x>>2);
then it will first print the o/p of x>>2 then x<<2
and then x or x then x<<2 and then x>>2.

plz clear my this doubt also.

from
amit
>
>
>


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
taxikaps Posted: Tue Dec 21, 2004 11:39 am


Joined: 03 Jan 2005

Posts: 10
Re: Digest Number
Hi Ashwin,

>>In case your book did not mention it, the shift
>>operator should be used on
>>unsigned value.
>>
>>I suggest you read what the standard said when using
>>the bitwise shift
>>operator on signed value:
>> <http://dev.unicals.com/c99-draft.html#6.5.7>
>>
>>
>>
>
> Hi Shyam,
>
> Can you please explain this the same procedure when
> int x = -5;
>
> Regards,
> Ashwin Mittal.
>

DID YOU BOTHER TO READ SHYAN`S COMPLETE MAIL. JUST SEE THE STATEMENTS
ABOVE FROM SHYAN.

Kapil
--
PS:
1. Google: Feel free to Google before posting. http://www.google.com

2. Posting: I suggest the group members to avoid top posting, as this
pushes the original question to the bottom. While any body would prefer
to have the main question at the top, and subsequent replies added to
the bottom, so that it is easy to comprehend the flow of information.

3. Do not Hijack threads. If you need to ask a fresh question, do a
little effort of moving mouse to the New or Compose button instead of
Reply. Do not start a New message or thread by hitting "Reply" in your
mail client and changing the subject. When you do this, you mess up
other participant`s ability to read mail in a threaded fashion.

4. HTML Mail: HTML is not email, and email doesn`t contain HTML, so
please turn HTML formatting OFF in your email client. Please Do not use
italics, colors, bold, fonts, pictures, sounds, or other HTML elements.
Please use only 7-bit text when sending email to the lists.

Thanks
Reply with quote
Send private message
View user's profile Post To page top
sflam108 Posted: Wed Dec 22, 2004 12:00 am


Joined: 25 Jul 2003

Posts: 48
Re: Digest Number
Hi,

> -----Original Message-----
> From: ashwin mittal [mailto:ashwin_mittal26@...]
> Sent: Monday, December 20, 2004 9:53 PM
> To: Programmers-Town@yahoogroups.com
> Subject: RE: (PT) Digest Number
>
>
> Hi Shyam,
>
> Can you please explain this the same procedure when
> int x = -5;

Did you read the link that I give (close to the end) in the previous reply?


In case you missed it, I reprint them here:

[quote]

I suggest you read what the standard said when using the bitwise shift
operator on signed value:
<http://dev.unicals.com/c99-draft.html#6.5.7>

[/quote]

It is either "undefined" or "implementation defined". You have to consult
the behavior to your compiler.

Shyan
Reply with quote
Send private message
View user's profile Post To page top
sflam108 Posted: Wed Dec 22, 2004 11:38 pm


Joined: 25 Jul 2003

Posts: 48
Re: Digest Number
Obviously he/she didn`t :(

> -----Original Message-----
> From: Kapil [mailto:taxikaps@...]
> Sent: Monday, December 20, 2004 11:39 PM
> To: Programmers-Town@yahoogroups.com
> Subject: Re: (PT) Digest Number
>
>
> DID YOU BOTHER TO READ SHYAN`S COMPLETE MAIL. JUST SEE THE STATEMENTS
> ABOVE FROM SHYAN.
>
Reply with quote
Send private message
View user's profile Post To page top
ashwin_mittal26 Posted: Thu Dec 23, 2004 6:52 pm


Joined: 27 Dec 2004

Posts: 14
Re: Digest Number
>
> hi frnds,
>
> u will be getting the ans 5 , 20 and 1 . but the ans
> given is in the format of 5,1 and 20.
>
> plz clear my doubt .
>
> i have one more doubt , from which side does the C
> starts processing the data.i.e
> suppose this is the statement given to u.
> printf("%d %d %d ",x,x<<2,x>>2);
> then it will first print the o/p of x>>2 then x<<2
> and then x or x then x<<2 and then x>>2.
>
> plz clear my this doubt also.
>
> from
> amit
> >
> >
> >
HI Amit,

printf takes(processes) the argument from right to
left which u urself have mentioned. and the answers
is 5, 20 & 1.. It dispays the data in the same form in
which it is demanded...

Regards,
Ashwin Mittal.




__________________________________
Do you Yahoo!?
Jazz up your holiday email with celebrity designs. Learn more.
http://celebrity.mail.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
sflam108 Posted: Thu Dec 23, 2004 10:36 pm


Joined: 25 Jul 2003

Posts: 48
Re: Digest Number
Reply embedded...

> -----Original Message-----
> From: harry potter [mailto:mummy_returns_arnold@...]
> Sent: Monday, December 20, 2004 11:40 PM
> To: Programmers-Town@yahoogroups.com
> Subject: RE: (PT) Digest Number( can any body tell me abt the order)
>
> hi frnds,
>
> u will be getting the ans 5 , 20 and 1 . but the ans
> given is in the format of 5,1 and 20.

I suppose the OP type it wrong. Or his compiler is broken.

A note for anyone posting codes and run results: To ensure we are reading
what you actually typed, please copy and paste codes from the compiler
instead of typing. As for run results, please copy and paste from the
command prompt (for Windows) or use redirect to a text file (if you use
stdout), and then copy and paste from the text file.

As for source code, please use spaces for indentation instead of tab.
Single line comments usually doesn`t works well with email (they got wrap
around), so use /* ... */


> plz clear my doubt .
>
> i have one more doubt ,

`Doubt` means you have some idea but not sure. I would really like to hear
what you think, and explain your "doubt" so that we can clear it. Or you
simply have a "question"?


> from which side does the C
> starts processing the data.i.e

"Data" is a very ambiguous term. What is your context? Rephrase and
explain what your real concern is. If you don`t know the exact terminology,
explain it with example.


> suppose this is the statement given to u.
> printf("%d %d %d ",x,x<<2,x>>2);
> then it will first print the o/p of x>>2 then x<<2
> and then x or x then x<<2 and then x>>2.

You`re talking about order of evaluation of function arguments, or how
printf() works?

For evaluation of function arguments:

Function arguments are not evaluated in any particular order, period. It
has nothing to do with "stack" or "push order".

C99 drift: 6.5.2.2p10:
The order of evaluation of the function designator, the actual
arguments, and subexpressions within the actual arguments is
unspecified, but there is a sequence point before the actual call.

Anybody tells you that function arguments are evaluated right-to-left
because function arguments are pushed right-to-left doesn`t know C.

For function call, this is what happens:
1. The arguments are evaluated, no in any particular order
2. All side effects are completed.
3. Call the function.

Inside printf() or any functions, it sees only the evaluated arguments.

For printf(), it will parse the format specifier, either output the
characters verbatim or format the passed argument as specify in the format
specifier.

So for example:

printf("%d %d %d ",x, x<<2, x>>2);
^ ^ ^ - ---- ----
| | | | | |
+--|--|---+ | |
+--|--------+ |
+-------------+

printf() would output the following:
- Value of `x` in decimal,
- One space,
- Value of the result of `x<<2` in decimal,
- One space,
- Value of the result of `x>>2` in decimal,
- One space.

Note that on some system, you might not get any output if your did not end
the format string with `
`.

HTH
Shyan
Reply with quote
Send private message
View user's profile Post To page top
shabble Posted: Wed Dec 29, 2004 5:44 pm


Joined: 29 Dec 2004

Posts: 31
Re: Digest Number
From: ashwin mittal [mailto:ashwin_mittal26@...]
>From: amit
>> hi frnds,
>>
>> u will be getting the ans 5 , 20 and 1 . but the ans
>> given is in the format of 5,1 and 20.
>>
>> plz clear my doubt .
>>
>> i have one more doubt , from which side does the C
>> starts processing the data.i.e
>> suppose this is the statement given to u.
>> printf("%d %d %d ",x,x<<2,x>>2);
>> then it will first print the o/p of x>>2 then x<<2
>> and then x or x then x<<2 and then x>>2.

It works out all three values first, then passes these results to
printf() - there is no predefined `order` in which these are called.

if x == 5 then

x<<2 == 5*2*2 == 10*2 == 20

x>>2 == (5/2)/2 == 2/2 == 1

>printf takes(processes) the argument from right to
>left which u urself have mentioned.

Not necesarily.

>and the answers
>is 5, 20 & 1.. It dispays the data in the same form in
>which it is demanded...

--
PJH

Quantum materiae materietur marmota monax si marmota monax materiam
possit materiari?





Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272

Website : www.alderley.com Sales : sales@... Service :
service@...

This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...

This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Reply with quote
Send private message
View user's profile Post To page top
skumaran_1980 Posted: Fri Dec 31, 2004 1:26 pm


Joined: 31 Dec 2004

Posts: 2
Re: Digest Number
Hi Ashwin & Paul,
 
I think the answer given by Paul is correct. But i have a different reasoning for that answer. Normally C process the data from right to left for eg:printf("%d %d %d ",x,x<<2,x>>2);This behaviour is happens in majority of the cases but also from left to right processing is possible. Basically this direction of processing is guided by the way in which stack is implemented.
 
Regards,
S. Kumaran.
Paul Herring <pherring@...> wrote:
From: ashwin mittal [mailto:ashwin_mittal26@...] >From: amit>> hi frnds,>> >> u will be getting the ans 5 , 20 and 1 . but the ans>> given is in the format of 5,1 and 20.>> >> plz clear my doubt . >> >> i have one more doubt , from which side does the   C>> starts processing the data.i.e >> suppose this is the statement given to u.>> printf("%d %d %d ",x,x<<2,x>>2);>> then it will first print the o/p of x>>2 then x<<2 >> and then x  or x then x<<2 and then x>>2.It works out all three values first, then passes these results toprintf() - there is no predefined `order` in which these are called.if x == 5 thenx<<2 == 5*2*2 == 10*2 == 20x>>2 ==
(5/2)/2 == 2/2 == 1>printf takes(processes) the argument from right to>left which u urself have mentioned. Not necesarily.>and  the answers>is 5, 20 & 1.. It dispays the data in the same form in>which it is demanded...--PJHQuantum materiae materietur marmota monax si marmota monax materiampossit materiari?Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12 8JD, UKTel: +44(0)1454 294556 Fax: +44 (0)1454 299272Website : www.alderley.com  Sales : sales@... Service : service@...This email and its contents are confidential and are solely for the use of the intended recipient. If you are not the original recipient you have received it in error and any use, dissemination, forwarding, printing or copying of this email is strictly prohibited. Should you receive this email in error please immediately notify
it@...This email has been scanned for viruses, however you should always scan emails with your own systems prior to opening.To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Do you Yahoo!? Jazz up your holiday email with celebrity designs. Learn more.
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help