2

What are the best methods to "Clear the 6th bit" of an integer?

And, is your solution platform independent? (32 or 64 bit integer, etc). If not, can you give a solution that is platform independent?

Update: we don't know whether that bit is set or unset when it was given... also, any language is ok... i know of a solution that is platform independent that requires 2 operators... maybe there are various methods or simpler solutions.

Update: more clearly: clear the 6th least significant bit of an interger.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • actually, any language... but if C is a good one for stating bitwise operations, or other language if they have very elegant solutions? – nonopolarity Mar 27 '10 at 00:02
  • 1
    Hmmm... which exactly is the 6th bit of a big-endian integer? And of a little-endian?... – Péter Török Mar 27 '10 at 00:02
  • @S.Lott: sounds like it. – Alan Mar 27 '10 at 00:06
  • @Lott actually, an interview question... for a frontend position... – nonopolarity Mar 27 '10 at 00:10
  • The most obvious solution requires at least two ops. I would go with the one you know. My solution uses more code but is a little more robust. Effectively, copy every bit but the X position to another int. Then return the new int. Yeah it's not super efficient, but it prevents loss of information, and on today's computers is easy enough. No requirement to do it in a certain number of ops or speed. Plus it's totally platform independent. Just think while (i != 0) j = ( i << 1 == 1 ) ? 1 : 0; return j; but there's no positional check here on purpose. Structure first. – jcolebrand Mar 27 '10 at 00:24
  • If this is a trick question: i = 0 would clear the sixth bit for sure. One operator, pretty language independent. – Chris Lercher Mar 27 '10 at 00:29
  • @chris_l hahaha... what about turning off the computer... yeah the question is "turn that bit off and that bit only, without affecting anything else"... – nonopolarity Mar 27 '10 at 00:37
  • @Jian: You never know - at least it was not in the (original version of the) question! It's what I'd answer first in an interview. Followed by what Greg suggests, because it's a lot faster than anything else: `~(1 << 6)` can be compiled into a static value - then the processor executes just the single AND operation with your value. – Chris Lercher Mar 27 '10 at 00:48
  • refer this link: http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c – kapilddit Jun 05 '12 at 13:57

4 Answers4

10

x & (~(1 << 6))

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
5

x&= ~(1 << 5)

MSN
  • 53,214
  • 7
  • 75
  • 105
0

Perhaps this in c?

value&~(1<<6)
spender
  • 117,338
  • 33
  • 229
  • 351
-2

If you're sure it's already set, just subtract 64.

Update: adding the test

if((x % 128) / 64 > 0)
  x -= 64;

As far as I can see right now, this doesn't make any assumptions about the platform or language support.

Ahmed Abdelkader
  • 1,695
  • 1
  • 13
  • 12
  • 1
    or 32 if you think that the low bit is the "1st" bit rather than the "0th" bit. ;) – John Knoeller Mar 27 '10 at 00:04
  • Jian Lin makes a very good point, you would have to check if the 6th bit is already set. Yours was my first thought too. – jcolebrand Mar 27 '10 at 00:20
  • This does both a modulo and a division to check if a bit is already set, when C and C++ already have bit manipulation operators that don't require that knowledge. – swestrup Mar 27 '10 at 02:34
  • Yes, what you noticed is obvious. This method was meant as an alternative. Try to think how to clear the bit without using bitwise operators. – Ahmed Abdelkader Mar 27 '10 at 03:54