473,396 Members | 1,774 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,396 software developers and data experts.

size of long

Hi all,
As an exercise, I am trying to figure out the size of a long in my
machine without using the sizeof operator. I came up with the
following:

int size_of_long(){
long i = 1,c = 1;
while(i > 0){
i<<=1;
c++;
}
return c / 8;
}

This works fine but I am afraid that if the code is being run in a
machine where it doesn't use 2's compliment. It will not work so I
came up with another function:

int size_of_long2(){
long i[2];
return (long)(i+1) - (long)i;
}

This works regardless of what machine the code is running. I wonder if
there is any other ways to determine the size of long?

Thanks!
Jul 22 '05 #1
7 5712
"pembed2003" <pe********@yahoo.com> wrote...
Hi all,
As an exercise, I am trying to figure out the size of a long in my
machine without using the sizeof operator. I came up with the
following:

int size_of_long(){
long i = 1,c = 1;
while(i > 0){
i<<=1;
c++;
}
return c / 8;
Why over 8? Shouldn't it be char_bits() or some such, implemented
the same way?
}

This works fine but I am afraid that if the code is being run in a
machine where it doesn't use 2's compliment. It will not work so I
came up with another function:

int size_of_long2(){
long i[2];
return (long)(i+1) - (long)i;
}

This works regardless of what machine the code is running. I wonder if
there is any other ways to determine the size of long?


Determine the size of 'unsigned long' and report it. IIRC, unsigned
versions of the arithmetic types have the same size as their signed
counterparts. For the 'unsigned long' the operation is well-defined
and they simply turn 0 after you shift them one time too much.

Victor
Jul 22 '05 #2
"pembed2003" <pe********@yahoo.com> wrote in message
As an exercise, I am trying to figure out the size of a long in my
machine without using the sizeof operator. I came up with the
following:


Without pre-processor: sizeof(long)


Jul 22 '05 #3
pembed2003 wrote:
Hi all,
As an exercise, I am trying to figure out the size of a long in my
machine without using the sizeof operator. [...]


Try

#include <iostream>
#include <limits>

int main() {
std::cout << std::numeric_limits<unsigned long int>::digits
<< std::endl;
}

This gives you the number of bits in "unsigned long int",
which is 32 on my system, for example. I'm not sure though
it's exactly synonymous with sizeof(unsigned long int).

HTH,
- J.
Jul 22 '05 #4
"Jacek Dziedzic" <ja*************@janowo.net> wrote...
pembed2003 wrote:
Hi all,
As an exercise, I am trying to figure out the size of a long in my
machine without using the sizeof operator. [...]


Try

#include <iostream>
#include <limits>

int main() {
std::cout << std::numeric_limits<unsigned long int>::digits
<< std::endl;
}

This gives you the number of bits in "unsigned long int",
which is 32 on my system, for example. I'm not sure though
it's exactly synonymous with sizeof(unsigned long int).


Of course it's not. It's synonymous with sizeof(unsigned long)*CHAR_BIT

Victor
Jul 22 '05 #5
pembed2003 wrote:
int size_of_long2(){
long i[2];
return (long)(i+1) - (long)i;
}

This works regardless of what machine the code is running.


Hmmm. Is it guaranteed that the compiler will not pad between elements
of an array of simple types?
Jul 22 '05 #6
Bill Seurer <se****@us.ibm.com> wrote:
pembed2003 wrote:
int size_of_long2(){
long i[2];
return (long)(i+1) - (long)i;
}

This works regardless of what machine the code is running.


Hmmm. Is it guaranteed that the compiler will not pad between elements
of an array of simple types?


Yes, but it's not guaranteed that it will convert from (long *) to (long).
Try:
return (char *)(i+1) - (char *)i;

Also, long i[1]; would have been sufficient (you're allowed to point
one-past-the-end of an array as long as you don't dereference).
Jul 22 '05 #7
Bill Seurer wrote:
Hmmm. Is it guaranteed that the compiler will not pad between elements
of an array of simple types?


Yes, it is! I'd asked that on this ng once, so now I know :)

- J.
Jul 22 '05 #8

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

Similar topics

8
by: Shailesh | last post by:
One problem I've been wrestling with for a long time is how to use the C++ integral data types, vis-a-vis their size. The C++ rules guarantee that a char is at least 1 bytes, a short and int at...
2
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
8
by: phil-news-nospam | last post by:
I have some code where I am using certain literal values cast to stdint types like uint32_t, uint64_t, etc. In gcc versions below 3.3 it's working OK. Here's an example: (uint64_t)...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
35
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : ...
3
by: spielmann | last post by:
Hello I want to change the scrollbar size of windows, How can I do that with vb.net I have find this in VB6 but how can we convert simply this code. thx
2
by: yxq | last post by:
Hello I want to get Windows clipboard data size, seem to use the function "GetClipboardDataSize". Could anyone please tell how to do using vb.net? Thanks
43
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
6
by: marktxx | last post by:
Although the C90 standard only mentions the use of 'signed int' and 'unsigned int' for bit-fields (use 'int' at your own risk) and C99 adds _Bool. It seems that most compilers create the size of...
7
by: John Fox | last post by:
Dear All, How do I set the size of the window that is showing the database forms? can't find any helps on it. John Fox
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.