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! 7 5715
"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
"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)
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.
"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
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?
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).
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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.
|
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)...
|
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...
|
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 : ...
|
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
|
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
|
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....
|
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...
|
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
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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: 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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |