Octal Integer Constants

G

Gary Brown

Hi,

I have a whole bunch of integer constants that are best given in octal
(PDP-1 opcodes). It makes a huge difference in readability and authenticity
if these
can be entered in octal. I haven't found a way to do this. Is there one?

Thanks,
Gary
 
L

Luc The Perverse

Gary Brown said:
Hi,

I have a whole bunch of integer constants that are best given in octal
(PDP-1 opcodes). It makes a huge difference in readability and
authenticity if these
can be entered in octal. I haven't found a way to do this. Is there one?

How about this:

const int O_11 = 9;
const int O_10167 = 4215;
 
P

Peter Duniho

I have a whole bunch of integer constants that are best given in octal
(PDP-1 opcodes). It makes a huge difference in readability and
authenticity
if these can be entered in octal. I haven't found a way to do this. Is
there one?

C# doesn't have octal literals. However, a couple of possible workarounds:

* VB does support octal literals (use "&O" before the number) so you
could define your constants in a VB.NET class and reference them from C#

* Depending on how the literals are to be used, you could just enter
them as hexadecimal literals that read visually as octal (sort of like
BCD) and then have some code that preprocesses them by taking out the
4-bit gaps in the binary values. I think this would work best if you're
using the literals to initialize an array or something like that, but even
if you're using them for named constants, you could "fake it" by instead
defining fields that are preprocessed at run-time (sorry if that sounds
oxymoronic...I hope you get what I mean :) ).

Using them as named constants, I think you'd have to gain a lot of
readability to justify the hack, since you'd sacrifice the minor
performance gain actual literal constants get you, as well as the
read-only nature of constants, and of course you'd have to have a block of
code somewhere that goes through and does the run-time preprocessing. All
of that's a pain; I would probably just write the hex, and if readability
was an issue I'd include the octal value in a comment next to it.

But for initializing an array, I think even the second method would work
pretty well. :)

Why you're looking for "authenticity" in source code, especially when
writing PDP-related code in C#, I'm not really clear on. But I'll take
your word for it that that's a desirable goal for some reason. :)

Pete
 
P

Peter Duniho

[...]
* Depending on how the literals are to be used, you could just
enter them as hexadecimal literals that read visually as octal (sort of
like BCD) and then have some code that preprocesses them by taking out
the 4-bit gaps in the binary values. [...]

Of course, I meant 1-bit gaps. Sorry if my mistyping confused things. :(
 
M

Michael A. Covington

Peter Duniho said:
[...]
* Depending on how the literals are to be used, you could just
enter them as hexadecimal literals that read visually as octal (sort of
like BCD) and then have some code that preprocesses them by taking out
the 4-bit gaps in the binary values. [...]

Of course, I meant 1-bit gaps. Sorry if my mistyping confused things. :(

Or (possibly better) enter them as strings and write your own routine to
convert them to integers in octal.
 
G

Gary Brown

I have a whole bunch of integer constants that are best given in octal
* Depending on how the literals are to be used, you could just enter
them as hexadecimal literals that read visually as octal (sort of like
BCD) and then have some code that preprocesses them by taking out the

That is essentially what I did. I had

const int add = 0400000,

which I transformed to a call

ConvertOp(SW, "add", 0400000);

with a macro. "Convert" converted the decimal 0400000 to hex then
output it with the mnemonic to a file. I pasted the file contents back
into the source yielding

const int add = 0x20000, // 0400000

Using Basic as someone suggested was also a good idea but I
don't have Basic installed.

It would have been so much easier if literals beginning with 0 were
octal as I originally thought.

Thanks,
Gary
 
M

Michael A. Covington

It would have been so much easier if literals beginning with 0 were
octal as I originally thought.

I think you've stumbled onto the only legitimate use of octal within the
past 30 years! :)
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Gary said:
Hi,

I have a whole bunch of integer constants that are best given in octal
(PDP-1 opcodes). It makes a huge difference in readability and authenticity
if these
can be entered in octal. I haven't found a way to do this. Is there one?

Surprisingly:

private const int c1 = 011;

does not work as expected.

But:

private static readonly int c2 = Convert.ToInt32("011", 8);

could be a possible workaround.

Arne
 

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

Top