C語(yǔ)言位操作源碼片段

如下的代碼段是關(guān)于C語(yǔ)言位操作片段的代碼。

為太和等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及太和網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、做網(wǎng)站、太和網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

  Copyright 2011 Shao-Chuan Wang <shaochuan.wang AT gmail.com>

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
#include <stdio.h>
#include <stdlib.h>

typedef struct {
  int size;
} packed_bits;

#define INT_MAX_BIT_MASK (1 << (INT_NBITS-1))

{
  if (!b_array)
    return NULL;
  p = malloc(sizeof(packed_bits));
  if (!p) {
    free(b_array);
    return NULL;
  }
  p->size = n_int;
  p->b = b_array;
  return p;
}

{
  int i;
  if (!p)
    return -1;
  b = p->b;
  for (i = p->size-1;i >= 0;i--) {
    b[i] = b[i] << 1;
    if (i-1 >=0 && b[i-1] & INT_MAX_BIT_MASK)
      b[i]++;
  }
  return 0;
}

{
  int i;
  if (!p)
    return -1;
  b = p->b;
  for (i = 0;i < p->size;i++) {
    b[i] = b[i] >> 1;
    if (i+1 < p->size && (b[i+1] & 1))
      b[i] |= INT_MAX_BIT_MASK;
  }
  return 0;  
}

{
  unsigned int offset = n % INT_NBITS;
  unsigned int idx = n / INT_NBITS;
  if (!p)
    return -1;
  b = p->b;
  return (b[idx] & (1 << offset)) != 0;
}

{
  unsigned int offset = n % INT_NBITS;
  unsigned int idx = n / INT_NBITS;
  if (!p)
    return -1;
  b = p->b;
  b[idx] |= (1<<offset);
  return 0;
}

{
  unsigned int offset = n % INT_NBITS;
  unsigned int idx = n / INT_NBITS;
  if (!p)
    return -1;
  b = p->b;
  b[idx] &= ~(1<<offset);

  return 0;
}

{
  int j;
  if (!p)
    return;
  b = p->b;
    printf("%d", read_bit(p, j));
    if (j % INT_NBITS==0)
      printf("n");
  }
  printf("n");
  return;
}

{
  int n_int = 4;
  if (!p) {
    fprintf(stderr, "Out of memory!n");
    return EXIT_FAILURE;
  }

  set_bit(p, 1);
  set_bit(p, 127);
  print_bits(p);

  return 0;
}

網(wǎng)頁(yè)題目:C語(yǔ)言位操作源碼片段
標(biāo)題鏈接:http://bm7419.com/article22/jcicjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、動(dòng)態(tài)網(wǎng)站、面包屑導(dǎo)航、App設(shè)計(jì)App開(kāi)發(fā)、

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司