Questions tagged [magic-numbers]

Magic numbers are unexplained values that often should be replaced with a named constant.

193 questions
608
votes
15 answers

What is a magic number, and why is it bad?

What is a magic number? Why should it be avoided? Are there cases where it's appropriate?
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
556
votes
7 answers

Is 23,148,855,308,184,500 a magic number, or sheer chance?

News reports such as this one indicate that the above number may have arisen as a programming bug. A man in the United States popped out to his local petrol station to buy a pack of cigarettes - only to find his card charged…
Roddy
  • 66,617
  • 42
  • 165
  • 277
314
votes
2 answers

What does the constant 0.0039215689 represent?

I keep seeing this constant pop up in various graphics header files 0.0039215689 It seems to have something to do with color maybe? Here is the first hit on Google: void RDP_G_SETFOGCOLOR(void) { Gfx.FogColor.R = _SHIFTR(w1, 24, 8) *…
crush
  • 16,713
  • 9
  • 59
  • 100
124
votes
10 answers

The cause of "bad magic number" error when loading a workspace and how to avoid it?

I tried to load my R workspace and received this error: Error: bad restore file magic number (file may be corrupted) -- no data loaded In addition: Warning message: file ‘WORKSPACE_Wedding_Weekend_September’ has magic number '#gets' Use of save…
N Brouwer
  • 4,778
  • 7
  • 30
  • 35
115
votes
3 answers

Magic number in boost::hash_combine

The boost::hash_combine template function takes a reference to a hash (called seed) and an object v. According to the docs, it combines seed with the hash of v by seed ^= hash_value(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); I can see that this…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
27
votes
11 answers

What are "magic numbers" in computer programming?

When people talk about the use of "magic numbers" in computer programming, what do they mean?
ckv
  • 10,539
  • 20
  • 100
  • 144
27
votes
3 answers

guess what 0x564c is?

when i read linux kernel source, i found one line says: #define NCP_SUPER_MAGIC 0x564c /* Guess, what 0x564c is :-) */ (564c)16 = (22092)10 = (53114)8 = (101011001001100)2 Wolfram|Alpha tells us that 22092 = 2^2 * 3 * 7 * 263 = 43…
Ted Yu
  • 1,784
  • 1
  • 10
  • 16
27
votes
3 answers

HowTo extract MimeType from a byte[]

I've a web page that that can be used to upload files. Now I need to check if the file type is correct (zip, jpg, pdf,...). I can use the mimeType that comes with the request but I don't trust the user and let's say I want to be sure that nobody is…
mickthompson
  • 5,442
  • 11
  • 47
  • 59
24
votes
3 answers

Express.JS: how can I set response status by name rather than number?

OK, everyone knows 200 is OK and 404 is not found. But I for things like permanent vs temporary redirect, or payment required, or other more exotic HTTP error codes, it might be better to do something…
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
22
votes
1 answer

Is 43679 a magic number?

Recently I was investigating this question: Storing long values in DataGridView C#. After some tests I found out that the DataGridViewCell will not display any text with a length larger than 43679 characters, even if the value is actually stored in…
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
17
votes
2 answers

How do I choose a good magic number for my file format?

I am designing a binary file format from scratch, and I would like to include some magic bytes at the beginning so that it can be identified easily. How do I go about choosing which bytes? I am not aware of any central registry of magic numbers, so…
jl6
  • 6,110
  • 7
  • 35
  • 65
16
votes
8 answers

How do I get the size of a file in megabytes using Perl?

I want to get the size of a file on disk in megabytes. Using the -s operator gives me the size in bytes, but I'm going to assume that then dividing this by a magic number is a bad idea: my $size_in_mb = (-s $fh) / (1024 * 1024); Should I just use…
cowgod
  • 8,626
  • 5
  • 40
  • 57
14
votes
2 answers

Killing Magic Numbers: "const int" vs "constexpr int" (or is there no difference in the end)

Let's say I have a magic number I want to get rid off... //whatever.cpp for (int i = 0; i < 42; i++) { //... } Reasonably I could kill it in two ways: Either with const int SOMETHING_SOMETHING_MEANING_OF_LIFE = 42 or with constexpr int…
CharonX
  • 2,130
  • 11
  • 33
14
votes
3 answers

How can I say a file is SVG without using a magic number?

An SVG file is basically an XML file so I could use the string
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
13
votes
4 answers

Why do we need a magic number in the beginning of the .class file?

I read a few posts here about the magic number 0xCAFEBABE in the beginning of each java .class file and wanted to know why it is needed - what is the purpose of this marking? Is it still needed anymore? or is it just for backwards compatibility now?…
RonK
  • 9,472
  • 8
  • 51
  • 87
1
2 3
12 13