Bit shifts and endianness

C

Chuck F.

Eric said:
Thank you, Jordan, for drawing my attention to my inattention.
Tracing back the thread, though, I find this remark from Mark
McIntyre:


.... to which you responded:


McIntyre says the shift operators work on values, not
representations, and you say you don't believe him. That's what
I'm trying to refute: I add my voice to the McIntyre (and
Heathfield) chorus to say that you are mistaken.

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
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
J

Jordan Abel

Thank you, Jordan, for drawing my attention to my
inattention. Tracing back the thread, though, I find
this remark from Mark McIntyre:


... to which you responded:


McIntyre says the shift operators work on values, not
representations, and you say you don't believe him.

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).
 
E

Eric Sosman

Chuck said:
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).

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.
 
R

Richard Heathfield

Jordan Abel said:
The statement i disbelieved was "Values don't have number bases".

Then your beef is with me.
The base of an integer value is 2.

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
hexadecimal.

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.
 
M

Mark McIntyre

The standard mentions "bits" in too many places for me to believe this.

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
 
M

Mark McIntyre

Something along the lines of:

An integer value in the range 0..2**(n-1) can be uniquely defined
as a sum of powers of two: ... + b3 * 2**3 + b2 * 2**2 + b1 * 2**1
+ b0 * 2**0. Each bn value is referred to as the nth _bit_ of the
integer value.

This is a representation of the value.
Mark McIntyre
 
R

Richard Heathfield

Mark McIntyre said:
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.

That fact alone should give you pause for thought. What makes you think my
statement is rubbish?
 
M

mensanator

Mark said:
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".

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?
 
G

Gordon Burditt

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?

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
 
S

slebetman

Jordan said:
<snip>

No. The statement i disbelieved was "Values don't have number bases".
The base of an integer value is 2.

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.
 
J

Joe Wright

Richard said:
Mark McIntyre said:




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

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?
 
R

Richard Heathfield

Joe Wright said:
Because 'stored' values do have number bases.

They do?
You seem to be playing games with language.

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".
Values in C stored in 'int x' have binary
representation in memory.

Representation, yes. I don't dispute that. So what? That's merely a
representation issue.
Whether 42, 052 or 0x2A the int in memory
looks something like 0..00101010 in binary.

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 value of 'x >> 1' will
look like 0..00010101 which seems to be a binary operation to me.

What am I missing?

The fact that we're not talking about representation here.
 
M

Mark McIntyre

Mark McIntyre said:


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

Because it was intrinsically meaningless.
Mark McIntyre
 
M

Mark McIntyre

Joe Wright said:


They do?

Yes. Obtuseness aside. Go ahead, though, design a baseless storage
mechanism and build a computer with it.... :)
It's just how it looks, not what it /is/.

Oh, philosophy now? Hmm, and we're not playing games?
The fact that we're not talking about representation here.

I'm afraid you are.
Mark McIntyre
 
R

Richard Heathfield

Mark McIntyre said:
Because it was intrinsically meaningless.

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.
 
R

Richard Heathfield

Mark McIntyre said:
Yes. Obtuseness aside. Go ahead, though, design a baseless storage
mechanism and build a computer with it.... :)

Oh, that's been done. Many times. Look up "analog computer".
Oh, philosophy now?

You can call it that if you want. But I call it mathematics.
Hmm, and we're not playing games?

Well, I'm not, but it seems you are.
I'm afraid you are.

No, we really really are not.
 
G

Gordon Burditt

Because 'stored' values do have number bases.

Representations have bases. Values do not.
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?

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
 
S

slebetman

Hmm, I posted a reply on this issue but it seems it disappeared. Google
bug?
Because 'stored' values do have number bases.

'Values' don't have number bases. Representations of the value do.
You seem to be playing games with language.

No, you seem to be playing games with language confusing the value of a
representation to the representation itself.
Values in C stored in 'int x' have binary representation in memory.

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.
Whether 42, 052 or 0x2A the int in memory looks something like 0..00101010
in binary.

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
The value of 'x >> 1' will look like 0..00010101 which seems to be a binary
operation to me.

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).
What am I missing?

Someone confused the word 'value' with 'representation' earlier in the
thread and is now arguing with Richard about it.

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).
 
S

slebetman

Hmm, I posted a reply on this issue but it seems it disappeared. Google
bug?
Because 'stored' values do have number bases.

'Values' don't have number bases. Representations of the value do.
You seem to be playing games with language.

No, you seem to be playing games with language confusing the value of a
representation to the representation itself.
Values in C stored in 'int x' have binary representation in memory.

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.
Whether 42, 052 or 0x2A the int in memory looks something like 0..00101010
in binary.

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
The value of 'x >> 1' will look like 0..00010101 which seems to be a binary
operation to me.

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).
What am I missing?

Someone confused the word 'value' with 'representation' earlier in the
thread and is now arguing with Richard about it.

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).
 

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

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top