1

I don't know how to proceed with this... I want to change a binary stored as an int 1111 to 111 being stored as an int also?

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
BOB LAT
  • 41
  • 1
  • 4
  • 1
    Please read up on [how to ask a good question](http://stackoverflow.com/help/how-to-ask). – Tom Zych Nov 15 '15 at 13:54
  • 1
    The number of bits in a number stored in a computer's memory (or a CPU's registers) is fixed, so you should set the bits you want to "remove" to 0: `0111` = `111`. – user4520 Nov 15 '15 at 14:00
  • Possible duplicate of [How do you set only certain bits of a byte in C without affecting the rest?](http://stackoverflow.com/questions/4439078/how-do-you-set-only-certain-bits-of-a-byte-in-c-without-affecting-the-rest) – try-catch-finally Nov 15 '15 at 15:09
  • @try-catch-finally: Not really a duplicate of that; this one needs to remove the most significant 1 bit, wherever that happens to be. – Tom Zych Nov 15 '15 at 15:41
  • @TomZych: is the most significant bit not _a certain bit_ or what? As the question is at the moment, I can't see that OP wants his code to be flexible to unset the 6th bit in `111111` too, with the same algorithm. Further, four bits aren't an integer so _1_111 is not the most significant bit. If the OP wants his code to unset _1_11111 too, he should mention it. `.oO( luckily the comment says "possible dupicate" )` Maybe [this is a better duplicate](http://stackoverflow.com/questions/31233452/set-most-significant-bit-in-c) :) – try-catch-finally Nov 15 '15 at 15:54
  • @try-catch-finally: Well, yes, I assumed a generalization of what the OP asked, mostly based on what the title says. Not an unreasonable guess at what the OP would have asked if they'd managed to be more coherent, I think. – Tom Zych Nov 15 '15 at 16:02

2 Answers2

0

I don't ordinarily answer "gimme teh codez" questions, but it was an interesting problem so I did it for fun. As usual, most of the time went into extraneous stuff like the output code.

If this is homework, do take the time to understand how the code works, or you'll only be cheating yourself.

#include <stdio.h>
#include <string.h>

// Define "number" as an unsigned number of desired size
typedef unsigned long number;

number drop_msb(number n);
char *ntob(char *dest, number n, int min_len);

int main()
{
    number i;
    number j;
    char ibuf[65];
    char jbuf[65];

    for (i = 0; i < 512; i++) {
        j = drop_msb(i);
        ntob(ibuf, i, 0);
        ntob(jbuf, j, strlen(ibuf) - 1);
        printf("%s --> %s\n", ibuf, jbuf);
    }

    return 0;
}

number drop_msb(number n)
{
    number bit;
    number a;

    // Handle special case
    if (n == 0)
        return 0;

    // Set highest bit
    bit = ((number) -1 >> 1) ^ (number) -1;

    // Guaranteed to terminate
    while (1) {
        a = n ^ bit;
        if (a < n)
            return a;
        bit >>= 1;
    }
}

char *ntob(char *dest, number n, int min_len)
{
    /* Convert n to shortest binary string, padding with zeroes on left
     * to make it at least min_len characters long. dest should be long
     * enough to hold the maximum number, plus terminating null. */

    char *left;
    char *right;

    /* min_len should be >= 1, to handle n == 0 correctly. Also needs to
     * be non-negative to avoid bad pointer during padding. */
    if (min_len < 1)
        min_len = 1;

    // Build with lsb on left
    for (right = dest; n; right++, n >>= 1)
        *right = '0' | (n & 1);

    // Pad if needed
    while (right < dest + min_len)
        *right++ = '0';

    *right = '\0';

    // Reverse it
    for (left = dest, right--; left < right; left++, right--) {
        *left ^= *right;
        *right ^= *left;
        *left ^= *right;
    }

    return dest;
}
Tom Zych
  • 13,329
  • 9
  • 36
  • 53
0
unsigned int test(unsigned int n)
{
unsigned int answer = 1;

while(n>>=1 && n)
{
    answer <<= 1;
} 
answer = (answer-1);
return answer;
}

This should solve your problem.

share75
  • 69
  • 1
  • 10