• Careercup | Chapter 5


    5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. Write a method to insert M into Nsuch that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all ofM. That is, ifM= 10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j-3 and i=2, because M could not fully fit between bit 3 and bit 2.

    update某一位的时候,记得要把该位清零先。

    Method 1: mask = ~((1<<(i-j+1)-1)<<i)

    Method 2:mask=(~0<<(j+1)) | ((1<<i) - 1)

    5.2 Given a real number between 0 and 7 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print "ERROR."

    小数的表示啊。用除法得到的是reverse的串。用乘法可以得到正串。

    5.3 Given a positive integer, print the next smallest and the next largest number that have the same number of 7 bits in their binary representation.

    用位操作方法很直观,用算术方法比较巧妙一些。arithmetically。 

    getNext: n + (1 << c0) + (1 << (c1-1)) - 1;

    getPrev: n - (1 << c1) - (1 << (c0-1)) + 1; 

     5.4 Explain what the following code does: ((n & (n-1)) == 0).

    check一下n是不是2的k次方。

    也可以用来检验二进制中1的个数。

    5.5 Write a function to determine the number of bits required to convert integer A to integer B.

    就是用n&(n-1)来计算。这个操作每次可以把least significant bit为1的给清零。

    5.6 Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g., bit 0 and bit! are swapped, bit 2 and bit 3 are swapped, and so on)

    巧妙。移偶数位,移奇数位。

    5.7 An array A contains all the integers from 0 through n, except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, and the only operation we can use to access them is "fetch thejth bit ofAfi]," which takes constant time. Write code to find the missing integer. Can you do it in 0(n) time?

    最简单的做法就是累加0到n,用求和公式得到的和减去计算出来的和,得到missing number。复杂度是O(nlgn)。因为n有lgn+1个bit。其实lgn一般也就32,如果n>>32,当然这种算法还是可以的。

    所以还是要二分,想到了要按位计数,但是没有想到比较0的个数和1的个数。

    在确定了LSBi的值后,需要去掉那些不符合的结果。

    5.8 A monochrome screen is stored as a single array of bytes, allowing eight consecutive pixels to be stored in one byte. The screen has width w, where w is divisible by 8 (that is, no byte will be split across rows). The height of the screen, of course, can be derived from the length of the array and the width. Implement a function drawHorizontalLine(byte[] screen, int width, intxl, intx2, inty) which draws a horizontal line from (x 1, y) to (x2, y).

     1 string binary(int n) {
     2     stringstream s;
     3 
     4     //while (n) { // negative number will always refill 1s for the most significant digit 
     5     for (int i = 0; i < 32; ++i) {
     6         s << (char)('0' + (n & 0x01));
     7         //cout << (char)('0' + (n & 0x01)) << endl;
     8         //cout << n << endl;
     9         n = n >> 1;
    10     }
    11     string ret = s.str();
    12     int i = 0, j = ret.length() - 1;
    13     while (j > i) {
    14         swap(ret[i], ret[j]);
    15         i++; j--;
    16     }
    17     return ret;
    18 }
    19 
    20 // 5.3
    21 int getNextSmallest(int n) {
    22     int i = 0, c1 = 0;
    23     for (; i <= 31 && (n & (1 << i)) == 0; ++i); 
    24     for (; (n & (1 << i)) != 0; ++i, ++c1);
    25     if (i > 31) return -1; 
    26     n |= 1 << i; // set i-th bit
    27     n &= ~0 << i;
    28     n |= ((1 << (c1 - 1)) - 1);
    29     return n;
    30 }
    31 
    32 int getPrevLargest(int n) {
    33     // example: 11100111
    34     int i = 0, c1 = 0;
    35     for (; (n & (1 << i)) != 0; ++i, ++c1); // ignore 1s, c1=3
    36     for (; i <= 31 && (n & (1 << i)) == 0; ++i); // find first 1, i=5
    37     if (i > 31) return -1; // error, 00001111 
    38     n &= ~0 << (i + 1); // clear i-th bit, n=11000000
    39     n |= ((1 << (c1 + 1)) - 1) << (i - c1 - 1); //n=11011110
    40     return n;
    41 }
    42 
    43 // 5.5 
    44 int diffBits(int a, int b) {
    45     int count = 0;
    46     for (int n = a ^ b; n != 0; n = n & (n - 1)) {
    47         count++;
    48     }
    49     return count;
    50 }
    51 
    52 // 5.6
    53 int swapBits(int n) {
    54     int ret = 0;
    55     for (int i = 0; i < 32; i += 2)  {
    56         int tmp = (n >> i) & 0x03;
    57         switch (tmp) {
    58             case 1: ret |= (2 << i); break;
    59             case 2: ret |= (1 << i); break;
    60             case 3: ret |= (3 << i); break;
    61         }
    62     }
    63     return ret;
    64 }
    65 
    66 int swapBits2(int n) {
    67     return ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1);
    68 }
    69 
    70 // 5.8
    71 void drawLine(char* screen, int width, int x1, int x2, int y) {
    72     int p1 = y * width + x1, p2 = y * width + x2;
    73     int b1 = p1 >> 3, b2 = p2 >> 3; //bucket
    74     int s1 = p1 & 0x07, s2 = p1 & 0x07; //shift
    75     for (int i = b1 + 1; i < b2; ++i) {
    76         screen[i] = 0xff;
    77     }
    78     int m1 = 0xff >> s1, m2 = ~(0xff >> (s2 + 1)); //mask
    79     if (b1 == b2) {
    80         screen[b1] |= (m1 & m2);
    81     } else {
    82         screen[b1] |= m1;
    83         screen[b2] |= m2;
    84     }
    85 }
  • 相关阅读:
    (SenchaTouch+PhoneGap)开发笔记(2)开发环境搭建二
    Sql语句复习
    冒泡排序
    微信开发订阅号(ASP.NET MVC4+jquery mobile+AppHarbor发布)
    Ext4 ComboBox组件使用
    ExtJs 进度条(轮询)
    如何替换掉.net toolStrip控件溢出按钮背景图
    easyui-menu 宽度自适应
    Python之入门学习
    servlet和filter的区别
  • 原文地址:https://www.cnblogs.com/linyx/p/3794402.html
Copyright © 2020-2023  润新知