Skip to content

Commit b308c76

Browse files
authored
Update bmpln.c
1 parent c34bbb9 commit b308c76

File tree

1 file changed

+10
-30
lines changed

1 file changed

+10
-30
lines changed

bmpln.c

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@
22

33
int isOdd(unsigned int x){ // Returns 1 if Odd, 0 if even // printf("%s", isOdd(a) ? "Odd" : "Even");
44

5-
if(x & 1u){
6-
return 1;
7-
}
8-
return 0;
5+
return (x & 1u);
96
}
107

118
/*****************************************************************************************************/
129

1310
int areOppSigns(int x, int y){ // Returns 1 if Opp, 0 if Same
1411

15-
16-
if ((x ^ y) < 0){ // Opp sign
17-
return 1;
18-
}
19-
return 0;
12+
return ((x ^ y) < 0);
2013
}
2114

2215
/*****************************************************************************************************/
@@ -29,23 +22,17 @@ int areOppSigns(int x, int y){ // Returns 1 if Opp, 0 if Same
2922

3023
int addOne(int x){ // Returns result
3124

32-
int ans;
33-
ans = -(~x); // MTA: ~ operator is bitwise NOT AKA 1s-complement
34-
return ans;
25+
return (-(~x));
3526
}
3627

37-
/*
38-
MTA:
39-
two's complement = ~number + 1
40-
*/
4128
/*****************************************************************************************************/
4229

4330
/******** Swap two numbers w/o third variable **********/
4431

4532
// x ^ x = 0
4633
// x ^ 0 = x
4734

48-
void Swap(int * x, int * y){
35+
void Swap(int *x, int *y){
4936

5037
*x = *x ^ *y;
5138
*y = *x ^ *y;
@@ -58,7 +45,7 @@ void Swap(int * x, int * y){
5845

5946
/******* Turn off kth bit in a number ***************/
6047

61-
// make a MACRO that takes in the position k, and makes a MASK for that position
48+
// make a MACRO that takes in the position k, and makes a Mask for that position
6249

6350
#define MASK(k) (1u << (k))
6451

@@ -74,11 +61,9 @@ int kthOff (int x, int k){
7461

7562
/******* Turn on kth bit in a number ***************/
7663

77-
//#ifndef MASK(k) // ifndef doesn't work on MACROFUNCTION ?
64+
//#ifndef MASK
7865
//#define MASK(k) (1u << k)
7966

80-
//#endif
81-
8267
// using the same MACROFUNCTION created above
8368

8469
void kthOn (int* x, int k){
@@ -87,6 +72,7 @@ void kthOn (int* x, int k){
8772

8873
return;
8974
}
75+
//#endif
9076

9177
/*****************************************************************************************************/
9278

@@ -132,8 +118,6 @@ int unsetrightmostset (int x){ // Returns result
132118

133119
int ispwroftwo (int x){ // Returns 1 if true, 0 false
134120

135-
//(x & (x-1)) ? return (0) : return (1); //error: expected expression before return
136-
137121
if(x&(x-1))
138122
return 0;
139123
else
@@ -149,9 +133,8 @@ int ispwroftwo (int x){ // Returns 1 if true, 0 false
149133

150134
int rmostsetpos (int x){
151135

152-
int y = x;
153-
int pos = -1; // BCZ I want rmost-bit as pos = 0
154-
x &= x-1;
136+
int y = x & (x-1);
137+
int pos = -1; // LSB as pos = 0
155138
y ^= x;
156139

157140
while(y){
@@ -197,10 +180,7 @@ int countsetbits (int x){
197180

198181
unsigned int swEndn (unsigned int x){ // 0x 1234abcd ---> 0x cdab3412
199182

200-
x = (x >> 16) | (x << 16); // 0x abcd1234
201-
x = ((x & (0xff00ff00)) >> 8) | ((x & (0x00ff00ff)) << 8);
202-
203-
return x;
183+
return (((x >> 24)&(0x000000ff)) | ((x >> 8)&(0x0000ff00)) | ((x << 8)&(0x00ff0000)) | ((x << 24)&(0xff000000)));
204184
}
205185

206186
/*****************************************************************************************************/

0 commit comments

Comments
 (0)