Simplify this ?

A

Alan Krueger

Victor said:
Another advice (in addition to my post): typos are programmer's worst
enemies. It's quite possible to make a mistake in such a constant even
if you copy and paste it from a calculator or something. I recommend
using 1024*1024*1024 instead. The compiler will calculate it for you.
Besides, it's readable a bit better than 1034234582 (oops.. I guess
I mistyped)

Er, wouldn't it be better to use something more suitable to powers of
two, like hexadecimal?

1024 = 1024 = 0x400
1024*1024 = 1048576 = 0x100000
1024*1024*1024 = 1073741824 = 0x40000000
 
A

Alan Krueger

Julián Albo said:
The mega prefix binary style is now called mebi, bi for binary.

Personally, "mebibyte", "gibibyte", and "tebibyte" sound totally absurd.
I can't bring myself to say them.
 
A

Alan Krueger

Victor said:

Truly a well-considered, informative response.

To reuse an example, 0x40000000 is much easier to maintain than
1073541824. Without recalculating that number or looking it up, can you
spot the error I introduced typing in that number? If you can, you're
in a very small minority. By contrast, any change to the former would
be much more visible.

As an extreme example of this, I once saw a sequence of increasing
single-bit bitmasks encoded in decimal. Curiously, a typo had been
added by accident in the list, introducing odd behaviors elsewhere in
the code. Finding the typo was a pain in the ass, whereas encoding them
in hexadecimal would have made the error stand out and be immediately
detectable.
 
V

Victor Bazarov

Alan Krueger said:
Truly a well-considered, informative response.

To reuse an example, 0x40000000 is much easier to maintain than
1073541824.

Both 0x4000000000 and 1073541824 are worse than 1024*1024*1024. Period.
Without recalculating that number or looking it up, can you spot the
error I introduced typing in that number? If you can, you're in a very
small minority. By contrast, any change to the former would be much more
visible.

Nope. The number of zeros you have to type makes it just as bad.
 
O

Old Wolf

Victor said:
Both 0x4000000000 and 1073541824 are worse than 1024*1024*1024.
Period.

1024*1024*1024 is the worst of the three. On a 16-bit
system it will cause undefined behaviour (signed int
overflow).
 
O

Old Wolf

Victor said:
Both 0x4000000000 and 1073541824 are worse than 1024*1024*1024.
Period.

1024*1024*1024 is the worst of the three. On a 16-bit
system it will cause undefined behaviour (signed int
overflow).
 
M

Matt Parkins

Victor Bazarov said:

Alternatively you could use some little macros:

#define KILO(n) (n *1024)
#define MEGA(n) (n *(1024 *1024))
#define GIGA(n) (n *(1024 *1024 *1024))

if (size <GIGA(1))

or

if (size < MEGA(1024))

I'm just thinking readability really.

Matt
 
V

Victor Bazarov

Matt Parkins said:
Victor Bazarov said:
Alan Krueger said:
Victor Bazarov wrote:
[...]
if (size < 1073741824)

Another advice (in addition to my post): typos are programmer's worst
enemies. It's quite possible to make a mistake in such a constant even
if you copy and paste it from a calculator or something. I recommend
using 1024*1024*1024 instead. The compiler will calculate it for you.
Besides, it's readable a bit better than 1034234582 (oops.. I guess
I mistyped)

Er, wouldn't it be better to use something more suitable to powers of
two, like hexadecimal?

No.

Alternatively you could use some little macros:

#define KILO(n) (n *1024)

#define KILO(n) ((n) *1024)

And we could actually try to set a trend if we call them

KIBI
MEBI
GIBI

:)

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,041
Latest member
Oliwen826

Latest Threads

Top