How can I cancel the ON bit in a byte?

I read Joel's book, where he offered as an interview question:

Write a program to discard the “ON” bit in the given byte.

I can only think of a solution using C.

Asking here so you can show me how to do this on a Non C path (if possible)

+5
source share
17 answers

I require a trick. :) Reverting all bits means a trigger, but only bits that are on explicitly mean:

return 0;
+21
source

What exactly does this question mean?

. "ON" "ON", 0, . , .. 1s 0s 0s 1s, , NOT . C ~, . :
unsigned char b = 102;      /* 0x66, 01100110 */
unsigned char reverse = ~b; /* 0x99, 10011001 */
+14

?

reverse 1 0 ?

00001100 → 00110000, ? , , , 1 1? . 00110101 → 00101011?

, , x86:

; al is input register
; bl is output register

xor bl, bl      ; clear output

; first bit
rcl al, 1       ; rotate al through carry
rcr bl, 1       ; rotate carry into bl

; duplicate above 2-line statements 7 more times for the other bits

, .

+4

#:

byte ReverseByte(byte b)
{
    byte r = 0;
    for(int i=0; i<8; i++)
    {
        int mask = 1 << i;
        int bit = (b & mask) >> i;
        int reversedMask = bit << (7 - i);
        r |= (byte)reversedMask;
    }
    return r;
}

, , , , , .

, , , . , , , , - . , , , , , , .

+4

1 0 0 1, Ruby:

n = 0b11001100
~n

:

n = 0b11001100
eval("0b" + n.to_s(2).reverse)

, :

n = 123
count = 0
0.upto(8) { |i| count = count + n[i] }

♥ Ruby

+3

, , , "on", .

:

#include <stdio.h>

int countBits(unsigned char byte);

int main(){
  FILE* out = fopen( "bitcount.c" ,"w");

  int i;
  fprintf(out, "#include <stdio.h>\n#include <stdlib.h>\n#include <time.h>\n\n");

  fprintf(out, "int bitcount[256] = {");
  for(i=0;i<256;i++){
    fprintf(out, "%i", countBits((unsigned char)i));
    if( i < 255 ) fprintf(out, ", ");
  }
  fprintf(out, "};\n\n");

  fprintf(out, "int main(){\n");

  fprintf(out, "srand ( time(NULL) );\n");
  fprintf(out, "\tint num = rand() %% 256;\n");
  fprintf(out, "\tprintf(\"The byte %%i has %%i bits set to ON.\\n\", num, bitcount[num]);\n");

  fprintf(out, "\treturn 0;\n");
  fprintf(out, "}\n");
  fclose(out);

  return 0;
}

int countBits(unsigned char byte){
  unsigned char mask = 1;
  int count = 0;
  while(mask){
    if( mask&byte ) count++;
    mask <<= 1;
  }
  return count;
}
+3

Bit Hacks ( ) , C. , C ( Java), , . , Haskell ;)

+2

byte ReverseByte(byte b) { return b ^ 0xff; }

, ^ XOR , AND, .

+2

​​ OpenJDK, , . , Scheme, 32- 64- .: -)

32- :

public static int reverse(int i) {
    // HD, Figure 7-1
    i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
    i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
    i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
    i = (i << 24) | ((i & 0xff00) << 8) |
        ((i >>> 8) & 0xff00) | (i >>> 24);
    return i;
}

64- :

public static long reverse(long i) {
    // HD, Figure 7-1
    i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L;
    i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L;
    i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL;
    i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
    i = (i << 48) | ((i & 0xffff0000L) << 16) |
        ((i >>> 16) & 0xffff0000L) | (i >>> 48);
    return i;
}
+2

..

while (Read())
  Write(0);
+2

, , , "on" , .

+1

Haskell soln , , :

import Data.Bits
import Data.Int

i = 123::Int
i32 = 123::Int32
i64 = 123::Int64
var2 = 123::Integer

test1 = sho i
test2 = sho i32
test3 = sho i64
test4 = sho var2 -- Exception

sho i = putStrLn $ showBits i ++ "\n" ++ (showBits $complement i)
showBits  v = concatMap f (showBits2 v) where
   f False = "0"
   f True  = "1"
showBits2 v = map (testBit v) [0..(bitSize v - 1)]
+1

palmsey, eval:

n = 0b11001100
n.to_s(2).rjust(8, '0').reverse.to_i(2)

rjust , , , - 0b00101010 0b10101, 0b01010100. (, 8 .) .

+1

, C- , XOR NOT, :

bFlipped = -1 - bInput;
+1

, , Non C way ( )

, 10101010. 1s 0s ( ), XOR:

 10101010
^11111111
 --------
 01010101

- "Non C", .

, "ON" ... ( ) (, , ).

+1

-C-, , SLIB:

(define (bit-reverse k n)
  (do ((m (if (negative? n) (lognot n) n) (arithmetic-shift m -1))
       (k (+ -1 k) (+ -1 k))
       (rvs 0 (logior (arithmetic-shift rvs 1) (logand 1 m))))
      ((negative? k) (if (negative? n) (lognot rvs) rvs))))

(define (reverse-bit-field n start end)
  (define width (- end start))
  (let ((mask (lognot (ash -1 width))))
    (define zn (logand mask (arithmetic-shift n (- start))))
    (logior (arithmetic-shift (bit-reverse width zn) start)
            (logand (lognot (ash mask start)) n))))

C ( , ), ( , ):

int
bit_reverse(int k, int n)
{
    int m = n < 0 ? ~n : n;
    int rvs = 0;
    while (--k >= 0) {
        rvs = (rvs << 1) | (m & 1);
        m >>= 1;
    }
    return n < 0 ? ~rvs : rvs;
}

int
reverse_bit_field(int n, int start, int end)
{
    int width = end - start;
    int mask = ~(-1 << width);
    int zn = mask & (n >> start);
    return (bit_reverse(width, zn) << start) | (~(mask << start) & n);
}
0

Reverse bit. For example, we have a number represented by 01101011. Now, if we change the bits, this number will become 11010110. Now for this you must first know how to exchange two bits in quantity. Rearrange two bits into a number: - XOR both bits with one, and see if the results differ. If they do not match, then both bits will be the same in XOR as bits with XOR, and save it in the original number; Now for changing the number FOR I am less than Numberofbits / 2 swap (number, I, NumberOfBits-1st);

0
source

All Articles