Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octal number literals: When? Why? Ever? [closed]

Tags:

c

numbers

I have never used octal numbers in my code nor come across any code that used it (hexadecimal and bit twiddling notwithstanding).

I started programming in C/C++ about 1994 so maybe I'm too young for this? Does older code use octal? C includes support for these by prepending a 0, but where is the code that uses these base 8 number literals?

like image 802
Jared Updike Avatar asked Sep 04 '08 19:09

Jared Updike


2 Answers

I recently had to write network protocol code that accesses 3-bit fields. Octal comes in handy when you want to debug that.

Just for effect, can you tell me what the 3-bit fields of this are?

0x492492 

On the other hand, this same number in octal:

022222222 

Now, finally, in binary (in groups of 3):

010 010 010 010 010 010 010 010 
like image 197
Ben Collins Avatar answered Sep 21 '22 08:09

Ben Collins


The only place I come across octal literals these days is when dealing with the permission bits on files in Linux, which are normally represented as 3 octal digits, where each digit represents the permissions for the file owner, group and other users respectively.

e.g. 0755 (also just 755 with most command line tools) means the file owner has full permissions (read, write, execute), and the group and other users just have read and execute permissions.

Representing these bits in octal makes it easier to figure out what permissions are set. You can tell at a glance what 0755 means, but not 493 or 0x1ed.

like image 22
Chris AtLee Avatar answered Sep 20 '22 08:09

Chris AtLee