473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How to calculate size of an int without using the sizeof operator but using bitwise operator

Please do not use pointer arithmetic or for loops
Solution

Nov 14 '05 #1
13 8241
Manish_Ganvir wrote:
Please do not use pointer arithmetic or for loops
Solution


Set an unsigned int to 1, use the left shift operator << , test whether its
zero.

gtoomey
Nov 14 '05 #2
Is there any one line solution?

Nov 14 '05 #3
Manish_Ganvir wrote:

Please do not use pointer arithmetic or for loops
Solution


#include <stdio.h>

#define FAIRLY_LARGE 32767

int main(void)
{
char buf[FAIRLY_LARGE] = {0};
puts("Please enter the size of an int, in bytes.");
if(fgets(buf, FAIRLY_LARGE, stdin) != NULL)
{
~fputs("The size of an int, in bytes, is ", stdout);
puts(buf);
}
return 0;
}

Now here's a little puzzle for you in return:

RG8gWW91ciBPd24gSG9tZXdvcmshDQo=

Enjoy!
Nov 14 '05 #4
In article <95e0d849bcfa56da7920973f5dec1132
@localhost.talkaboutprogramming.com>, ma*******@rediffmail.com says...
Is there any one line solution?


The oracle declares that you owe 50 pounds of chocolate chips for
help with incredibly stupid homework assignments.

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
Nov 14 '05 #5
On Sat, 12 Feb 2005 04:31:19 -0500, in comp.lang.c , "Manish_Ganvir"
<ma*******@rediffmail.com> wrote:
Is there any one line solution?


To what? Its a good idea to put your actual question in the message body.

And the answer is perhaps.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #6
On Sat, 12 Feb 2005 04:31:19 -0500, in comp.lang.c , "Manish_Ganvir"
<ma*******@rediffmail.com> wrote:
Is there any one line solution?


To what? Its a good idea to put your actual question in the message body.

And the answer is perhaps.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #7
"Manish_Ganvir" <ma*******@rediffmail.com> writes:
Please do not use pointer arithmetic or for loops
Solution


Q: How do I compute the size of an int without using sizeof?
A: Use sizeof. That's what it's for.

Q: But how do I compute the size of an int *without using sizeof*?
A: Why would you want to do a silly thing like that?

Q: It's a homework assignment.
A: Then give us your instructor's e-mail address so we can submit the
solution directly. You wouldn't want to deny us credit for our
work, would you?

Q: But then how do I get class credit?
A: I guess you don't.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
On 2005-02-12 04:25:55 -0500, Gregory Toomey <no****@bigpond.com> said:
Manish_Ganvir wrote:
Please do not use pointer arithmetic or for loops
Solution


Set an unsigned int to 1, use the left shift operator << , test whether its
zero.


That won't always work (think padding-bits, trap representations, etc.).
--
Clark S. Cox, III
cl*******@gmail.com

Nov 14 '05 #9
Clark S. Cox III wrote:
On 2005-02-12 04:25:55 -0500, Gregory Toomey <no****@bigpond.com> said:
Manish_Ganvir wrote:
Please do not use pointer arithmetic or for loops
Solution

Set an unsigned int to 1, use the left shift operator << , test
whether its
zero.

That won't always work (think padding-bits, trap representations, etc.).


Not applicable. The shift operator works with
the values of its operands, not with their representation.
Left-shifting a 1 by 1 bit always produces 2, even if
forty-two padding bits lie between the units' and twos'
positions. Furthermore, shifting a valid unsigned
value (e.g., 1) always produces a valid unsigned value
and never produces a trap representation.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #10
Eric Sosman <es*****@acm-dot-org.invalid> writes:
Clark S. Cox III wrote:
On 2005-02-12 04:25:55 -0500, Gregory Toomey <no****@bigpond.com> said:
Manish_Ganvir wrote:

Please do not use pointer arithmetic or for loops
Solution
Set an unsigned int to 1, use the left shift operator << , test
whether its
zero.

That won't always work (think padding-bits, trap representations, etc.).


Not applicable. The shift operator works with
the values of its operands, not with their representation.


Right, and the question was about the representation, specifically the
size.

If you have a 32-bit integer type with 8 padding bits, any solution
using bitwise operations will tell you it has 24 bits. The question
was about its size, 32 bits, not its width.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11
It is a nice way to say that you dont know the solution, i really
appreciate your effort guys

Thanks

Nov 14 '05 #12
Manish_Ganvir wrote:

It is a nice way to say that you dont know the solution, i really
appreciate your effort guys


No quotes again. What does 'it' refer to? Solution to what? I
suspect that 'it' is really a toned down version of "stop wasting
our time".

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #13
"Manish_Ganvir" <ma*******@rediffmail.com> writes:
It is a nice way to say that you dont know the solution, i really
appreciate your effort guys


Following the References header indicates that this was a followup to
my article, but you didn't provide any context.

If you want to post a followup via groups.google.com, don't use the
broken "Reply" link at the bottom of the article. Click on "show
options" at the top of the article, then click on the "Reply" at the
bottom of the article headers.

Most of us either know a solution, or could figure one out if we spent
the time on it, or (depending on how your question is interpreted)
could demonstrate that no solution is possible. We simply aren't
interested in doing your homework for you.

If you have some valid reason for this rather odd question other than
a homework assignment, I'm sure many of us would be glad to help.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: littlehobo | last post by:
I'm trying to figure out this question i was asked but am having no luck! here's the question: "Write a Program that swaps the contents of two variables without using any other variable. Use one...
15
by: subramanian100in | last post by:
Is it possible to measure the size of an array without using the sizeof operator ?
5
by: Gianni Mariani | last post by:
I'm hoping someone can tell me why using member address of works and why using the dot operator does not in the code below. The code below uses the template function resolution mechanism to...
5
by: Junmin H. | last post by:
Hello, all. I never use them when I am programming. But i have been reading codes that using them. When should I use them? Why not use char/short/int/long? Is it very important to know them well?...
1
by: somenath | last post by:
Hello All, I was trying to learn bitwise operator in C .In that process I cam across one resource as mentioned bellow http://graphics.stanford.edu/~seander/bithacks.html#OperationCounting I...
9
by: Ioannis Vranos | last post by:
Well when we have an expression like this: int obj= first | second; Why is this style preferred than the equivalent: int obj= first+ second; ?
1
by: Charming12 | last post by:
Hi All, This might be looking a naive question, but i am facing a big issue because of this. I have a class written in C# .Net which contains one more class. And i need to pass the size of this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.