0

So I'm currently trying to finish some university homework that deals with bitwise manipulations, and one of the exercises is giving me the worst kind of trouble, the one where you dont know where you went wrong. The exercise is as follows:

Implement the function int activate_bits(int a, int left, int right ) that should ’activate’ all the bits to the left of left and to the right of right on the number a (excluding the bits left and right).

My code regarding the activate_bits function is the following

#include <stdio.h> unsigned int activate_bits(unsigned a, int left, int right){ int mask1 = 1; int mask2 = 1; int mask3 = 0; int i; /* assuming an int as only 8 bits for convenience, what I want to do here is shift the least significant bit to the left, then add 1,as many times as necessary(according to the parameter right), so that the number 00000001 becomes 00000011 and so forth */ for (i= (right -1); i<right ; i++){ mask1 << 1; mask1 = mask1 +1 ; } /* doing the same as above here, checking how many activated bits the second mask should actually have by doing (32 - left ) */ for (i = (32 - left); i < 0; i--){ mask2 << 1; mask2 = mask2 +1 ; } /* now I'm shifting the second mask as many times as needed so it is placed after the bit position indicated by the left parameter */ mask2 << left; mask3 = mask1 + mask2; return a | mask3; } 

Can anyone help me as to why this is giving me a wrong result ? Thanks in advance

1
  • for (i= (right -1); i<right ; i++){ is a 1-iteration loop with i=right-1, which is never used inside the loop. Commented Nov 19, 2016 at 19:06

2 Answers 2

2

mask1 << 1; doesn't do what you think it does. It's not like mask1++ which increments mask1; it's the same as having the line mask1 + 1 - the result is evaluated but not stored anywhere.

Try doing this:

mask1 = mask1 << 1

Or, for brevity's sake:

mask1 <<= 1

Sign up to request clarification or add additional context in comments.

7 Comments

mask<<1 is the same as mask*2.
This absolutely did it for me. I've been scratching my brains out for the past hour and a half about this, only to find out that it was actually quite a simple mistake. Well, I'm glad it's fixed. Thanks a bunch
@Govind Parmar, it was just an observation. Your solution is OK.
This fixes the error but the whole things is a very inefficient way of getting a mask. A mask with the lowest n bits set is unsigned low_mask=(1U<<n)-1;. Notice that one less than a power of 2 is a string of ones. That expression amounts to 2**(n) - 1. With a bit of thought and a 'bitwise not' (~) you can get a high-end mask by making an opposite low end mask.
@DanAllen, a mask with the lowest n bits set is (1U<<n)-1 (e.g. for n=1, we get 2-1=1, which indeed has 1 bit).
|
2

You can simplify the whole thing without any loops:

unsigned int activate_bits(int a, int left, int right) { unsigned int mask1; unsigned int mask2; unsigned int tmp; // left mask tmp = 0x01 << left; // e.g. if left is 15, then set bit #15 ... tmp=0x00008000 tmp = tmp - 1; // set bits 0-15, and left bit 15 cleared: 0x00008000 => 0x0007fff tmp = ~tmp; // invert the bits: 0x00007fff ===> 0xffff8000 tmp = tmp << 1; // clear bit 15 mask1 = tmp; // and that's the left mask // right mask tmp = 0x01 << right; // If right is 15, start by setting the 15 bit... tmp = tmp=0x00008000 tmp = tmp - 1; // clear bit 15 and set everything to the right of it. 0x00008000 ===> 0x00007fff; mask2 = tmp; return (a | mask1 | mask2); } 

2 Comments

The truly portable way uses CHAR_BIT in <limits.h> as the number of bits in a byte. Though it is 8 on modern platforms.
tmp ^ ALL_BITS_SET, aka ~tmp

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.