#include \"stdio.h\" #include \"stdlib.h\" #include \"sys/stat.h\" /* A review o
ID: 3664227 • Letter: #
Question
#include "stdio.h"
#include "stdlib.h"
#include "sys/stat.h"
/*
A review on binary, octal, and decimal numbers.
Converting from binary to base 10 ( decimal ):
Binary numbers are in this format ( for a 2 byte, 16-bit number ):
PONM LKJI HGFE DCBA
---- ---- ---- ----
A: 1's place H: 128's place
B: 2's place I: 256's place
C: 4's place J: 512's place
D: 8's place K: 1024's place
E: 16's place L: 2048's place
F: 32's place M: 4096's place
G: 64's place N: 8192's place
O: 16384's place
P: 32768's place
Note:
0 is a bit turned off.
1 is a bit turned on.
Let's convert the binary number
0000 1000 0101 0010
to base 10.
Basically, you sum up each place, that has a 1. So for the above,
we have 1 in 2048's place, 64's place, 16's place, and 2's place.
So we sum up all those places:
2048+64+16+2 = 2130
So, 0000100001010010 = 2130 base10.
Now, let's convert that to base8.
If you group the bits into groups of 3, the number could look like this:
0 000 100 001 010 010
Each full 3 bit section can have a value from 0 through 7. In each section:
ABC
---
A is the 4's place
B is the 2's place
C is the 1's place
So the above number, comes down to
0 000 100 001 010 010
0 0 4 1 2 2
So in base 8, the number is 4122.
So, the numbers, converted between bases looks like this:
0000100001010010 = 2130 = 4122
base2 base10 base8
binary decimal octal
0010 0000 32 base 10 in binary
00 100 000 40 base 8 in binary
S_IRGRP = 00040 base 8
*/
int main( int ac, char* av[] )
{
int mode = S_IRGRP; // mode contains 32 base 10 == 00040 base 8
printf("%i ", mode );
// let's turn the 1's bit, using bitwise | and base 8
mode = mode | 01;
printf("%i ", mode );
/*
updated bit pattern:
0010 0001 33 base 10 in binary
00 100 001 41 base 8 in binary
*/
// let's turn on the 16's bit, using bitwise | and base 8
mode = mode | 010;
printf("%i ", mode );
/*
updated bit pattern
0011 0001 41 base 10 in binary
00 110 001 61 base 8 in binary
*/
/*
let's determine if the 16's bit is turned on using bitwise &
and masking.
mode's current value: 41 base 10, 61 base 8
000 110 001 61 base 8
000 010 000 10 base 8
___________
000 010 000 16 base 8, =/= 0, therefore, true
*/
if ( mode & 010 )
printf("16's bit is on ");
else
printf("16's bit is off ");
/*
Ok, let's demonstrate turning off a bit.
Here, we'll use assignment and bitwise and to turn bits off.
000 110 001 61 base8
111 101 111 757 base8
&= ___________
000 101 001 51 base8, 41 base10
*/
mode = mode & 0757;
printf("%i ", mode );
return 0;
}
Explanation / Answer
#include #include #include #include #include int main(int argc, char **argv) { char mode[] = "0777"; char buf[100] = "/home/hello.t"; int i; i = strtol(mode, 0, 8); if (chmod (buf,i) < 0) { fprintf(stderr, "%s: error in chmod(%s, %s) - %d (%s) ", argv[0], buf, mode, errno, strerror(errno)); exit(1); } return(0); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.