# Bit shifts and endianness

Discussion in 'C Programming' started by gamehack, Jan 5, 2006.

1. ### Eric SosmanGuest

Woops! My apologies for the misattribution.

Eric Sosman, Jan 6, 2006

2. ### Chuck F.Guest

You can pick out any bit from a value, regardless of the underlying
representation, by dividing by a suitable power of 2, and then
taking the result modulo 2. The latter is simply the Pascal
function odd(n).

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the

Chuck F., Jan 6, 2006

3. ### Jordan AbelGuest

No. The statement i disbelieved was "Values don't have number bases".
The base of an integer value is 2. The base of a floating-point value is
FLT_RADIX (2 on my system, may be 10 on others for example).

Jordan Abel, Jan 6, 2006
4. ### Eric SosmanGuest

Right. And you can pick out a trit by dividing by a suitable
power of three and taking the result modulo 3. Or you can pick
out quits with fours, quints with fives, dits with tens, twits
with twelves, and so on. As Richard Heathfield says (misattributed
by me to Mark McIntyre), "values don't have number bases." No
matter how many fingers you have, pi is pi is pi.

Eric Sosman, Jan 6, 2006
5. ### Richard HeathfieldGuest

Jordan Abel said:
Then your beef is with me.
No, it's not, neither in mathematics nor in C.

int i = 42;

Here, the 42 represents an integer. The representation used is denary, also
known as decimal.

int j = 052;

Here, the 052 represents an integer. The representation used is octal.

int k = 0x2A;

Here, the 0x2A represents an integer. The representation used is

In each of these three cases, the value is the same: it's the number of
asterisks in the following collection:

*****
*******
*********
*********
*******
*****

We have seen the same integer value represented in three different bases,
none of which was binary, and in a non-positional representation as well.
Values *don't* have bases. Bases are what we use to /represent/ values.

Richard Heathfield, Jan 6, 2006
6. ### Mark McIntyreGuest

For some reason RJH's original post didn't make it here. However, my
response would have been " Rubbish", which is rare for a response to
an RJH post.

One can I suppose argue that you break the number down into a sum of
powers of two. Er, guys, thats called a "representation".

Mark McIntyre

Mark McIntyre, Jan 6, 2006
7. ### Mark McIntyreGuest

This is a representation of the value.
Mark McIntyre

Mark McIntyre, Jan 6, 2006
8. ### Richard HeathfieldGuest

Mark McIntyre said:
That fact alone should give you pause for thought. What makes you think my
statement is rubbish?

Richard Heathfield, Jan 7, 2006
9. ### mensanatorGuest

But you must pick a representation to create an "implementation",
right?

You could implement integer representations as strings of characters
"0" and "1" and have unlimited precision arithmetic. Am I correct in
assuming the standard would allow this as long as evrything behaves as
it should?

mensanator, Jan 7, 2006
10. ### Gordon BurdittGuest

But you must pick a representation to create an "implementation",
But it *WON'T* behave as it should. int can't be a variable size.
Also, there are practical and economic problems with sizeof(int) = infinity.

You could choose a large but finite size (say, 1 gigabit integers),
and that could be made to work.

Gordon L. Burditt

Gordon Burditt, Jan 7, 2006
11. ### slebetmanGuest

Actually, that's not correct. The base of the BINARY REPRESENTATION of
an integer is 2. The base of the decimal representation of an integer
is 10. The base of the hexadecimal representation of an integer is 16.
etc..

Values don't have number bases. That's only their representation.

slebetman, Jan 7, 2006
12. ### Joe WrightGuest

Because 'stored' values do have number bases. You seem to be playing
games with language. Values in C stored in 'int x' have binary
representation in memory. Whether 42, 052 or 0x2A the int in memory
looks something like 0..00101010 in binary. The value of 'x >> 1' will
look like 0..00010101 which seems to be a binary operation to me.

What am I missing?

Joe Wright, Jan 7, 2006
13. ### Richard HeathfieldGuest

Joe Wright said:
They do?
Not trying to, sir. Although C /is/ a language, sir. But the concept of
"number" is independent of and therefore does not rely on the concept of
"base".
Representation, yes. I don't dispute that. So what? That's merely a
representation issue.
And in bits, it looks like a really scary Feynman diagram, I expect. So?
It's just how it looks, not what it /is/.
The fact that we're not talking about representation here.

Richard Heathfield, Jan 7, 2006
14. ### Mark McIntyreGuest

Because it was intrinsically meaningless.
Mark McIntyre

Mark McIntyre, Jan 7, 2006
15. ### Mark McIntyreGuest

Yes. Obtuseness aside. Go ahead, though, design a baseless storage
mechanism and build a computer with it....
Oh, philosophy now? Hmm, and we're not playing games?
I'm afraid you are.
Mark McIntyre

Mark McIntyre, Jan 7, 2006
16. ### Richard HeathfieldGuest

Mark McIntyre said:
Well, Mark, I must disagree, and I think you'll find any mathematician worth
his salt disagreeing with you too. Values are indeed independent of number
base, whether you see meaning in that statement or not.

Richard Heathfield, Jan 8, 2006
17. ### Richard HeathfieldGuest

Mark McIntyre said:
Oh, that's been done. Many times. Look up "analog computer".
You can call it that if you want. But I call it mathematics.
Well, I'm not, but it seems you are.
No, we really really are not.

Richard Heathfield, Jan 8, 2006
18. ### Gordon BurdittGuest

Because 'stored' values do have number bases.

Representations have bases. Values do not.
If I add 5 in base two to 3 in base five, what base is the result
in? Why? If your answer is "any base you want it to be in", this
really suggests that the base is separate from the value.

Oh, yes, what base is the *BASE* in?

Gordon L. Burditt

Gordon Burditt, Jan 8, 2006
19. ### slebetmanGuest

Hmm, I posted a reply on this issue but it seems it disappeared. Google
bug?
'Values' don't have number bases. Representations of the value do.
No, you seem to be playing games with language confusing the value of a
representation to the representation itself.
Store in where? In a CPU register, then yes it most probably is binary.
In the graphics frame buffer it is a bunch of pixels related to the
font which when read by a human represents the value. In a file on disk
then it is probably an ASCII string representing the value.
Yes. And it looks something like 42 in decimal and 52 in octal and 2A
in hexadecimal. You used the word 'looks'. That should tell you that
you are only talking about representation, not the value itselt.

If you want to see how that value it looks like in the real world,
under the microscope, instead of binary then it will probably look like
this:

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|

of course you can't tell what the value is because all D flip-flop
cells look the same under the microscope. But if you were to attach
tiny LEDs to the outputs you'll see something like:

off, off, off .. on, off, on, off, on, off

But then again, that's merely a representation issue of the actual
voltages:

0V, 0V .. 5V, 0V, 5V, 0V, 5V, 0V

which, again, is merely a representation of the number 42
Well, we are talking about C here. And C doesn't define it as a binary
operation. C defines it as an arithmetic operation equivalent to
modulo2(x/2).
Someone confused the word 'value' with 'representation' earlier in the

Go back to what you learned in high school. A number 'base' defines a
'number system' used to represent a 'number'. Numbers (hence values) by
themselves don't have bases. But we need a way to write down
(represent) numbers in order to talk about them. Hence we need a number
system to describe numbers. A 'base' simply defines how much larger a
digit to the 'left' is compared to the current digit in that number.
Altering the base of a number system does not alter the 'value' of that
number itself. It merely alters the number system: how the number is
represented (otherwise I will find myself with fewer fingers if I
suddenly decide to count them in hex and more fingers if I decide to
count them in octal).

slebetman, Jan 8, 2006
20. ### slebetmanGuest

Hmm, I posted a reply on this issue but it seems it disappeared. Google
bug?
'Values' don't have number bases. Representations of the value do.
No, you seem to be playing games with language confusing the value of a
representation to the representation itself.
Store in where? In a CPU register, then yes it most probably is binary.
In the graphics frame buffer it is a bunch of pixels related to the
font which when read by a human represents the value. In a file on disk
then it is probably an ASCII string representing the value.
Yes. And it looks something like 42 in decimal and 52 in octal and 2A
in hexadecimal. You used the word 'looks'. That should tell you that
you are only talking about representation, not the value itselt.

If you want to see how that value it looks like in the real world,
under the microscope, instead of binary then it will probably look like
this:

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|

of course you can't tell what the value is because all D flip-flop
cells look the same under the microscope. But if you were to attach
tiny LEDs to the outputs you'll see something like:

off, off, off .. on, off, on, off, on, off

But then again, that's merely a representation issue of the actual
voltages:

0V, 0V .. 5V, 0V, 5V, 0V, 5V, 0V

which, again, is merely a representation of the number 42
Well, we are talking about C here. And C doesn't define it as a binary
operation. C defines it as an arithmetic operation equivalent to
modulo2(x/2).
Someone confused the word 'value' with 'representation' earlier in the

Go back to what you learned in high school. A number 'base' defines a
'number system' used to represent a 'number'. Numbers (hence values) by
themselves don't have bases. But we need a way to write down
(represent) numbers in order to talk about them. Hence we need a number
system to describe numbers. A 'base' simply defines how much larger a
digit to the 'left' is compared to the current digit in that number.
Altering the base of a number system does not alter the 'value' of that
number itself. It merely alters the number system: how the number is
represented (otherwise I will find myself with fewer fingers if I
suddenly decide to count them in hex and more fingers if I decide to
count them in octal).

slebetman, Jan 8, 2006