-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitops.c
90 lines (82 loc) · 2.79 KB
/
bitops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Copyright (C) 2012 Ben Lewis <benjf5+github@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
/* Function declarations */
unsigned setbits (unsigned x, int p, int n, unsigned y);
unsigned invert (unsigned x, int p, int n);
unsigned int rightrot (unsigned int x, int n);
/* K&R exercise 2-6 */
/* Write a function that returns x with the n bits that start at p replaced with
* the n rightmost bits of y, leaving all other bits unchanged.
*/
unsigned setbits (unsigned x, int p, int n, unsigned y) {
/* This function does not check inputs for correctness, so if you hand it
* nonsense values it will spit out nonsense values. Caveat Utilitor.
*/
return (x & ~(~(~0 << n) << (p - n))) | ((~(~0 << n) & y) << (p - n));
}
/* K&R exercise 2-7 */
/* Write a function that returns x with the n bits beginning at the position p
* inverted, leaving the rest unchanged.
*/
unsigned invert (unsigned x, int p, int n) {
/* sets n bits right of p to be the inverse of what they were initially. */
return x ^ ((~(~0 << n)) << (p - n));
}
/* K&R exercise 2-8 */
/* Write a function that returns the value of the integer x rotated by n bit
* positions.
*/
unsigned int rightrot (unsigned int x, int n) {
/* Returns x rotated by n bits */
unsigned int temp = ~(~0 << n) & x;
temp = temp << ((sizeof(unsigned int) *8) - n);
return (x >> n) | temp;
}
int main() {
unsigned char x, y, t, q;
unsigned int z, r;
/* x = 0010 1101
* x = 055
*/
x = '\x2D';
/* y = 1101 0001
* y = 321
*/
y = '\xD1';
/* This was before I realised that z has to be an int.
* z = 1001 0110
* z = '\x96';
*/
z = 5;
/* t is what setbits (x, 5, 3, y) should return, ie
* t = 0010 0101
* t = 045
*/
t = '\x25';
/* q is what invert (y, 4, 4) should return, ie
* q = 1101 1110
* q = 336
*/
q = '\xDE';
printf ("Expected setbits output: %o\n", t);
printf ("Actual setbits output: %o\n", setbits (x, 5, 3, y));
printf ("Expected invert output: %o\n", q);
printf ("Actual invert output: %o\n", invert (y, 4, 4));
printf ("z value: %o\n", z);
printf ("z, rotated by 3: %o\n", rightrot (z, 3));
}