473,378 Members | 1,512 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,378 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 5709
"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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.