存档

‘Programming’ 分类的存档

[整理]在C中操作int的连续某几位

2010年7月12日 Galaxy 1 条评论

翻同事的The C Programming Language 2th(C程序设计语言)和对应的习题解答The C Answer Book看到的。
实体书看得爽,但写文章还是得找电子版,英文的chm有,可惜习题解答没文字版……

The shift operators < < and >> perform left and right shifts of their left operand by the number of bit positions given by the right operand, which must be non-negative. Thus x < < 2 shifts the value of x by two positions, filling vacated bits with zero; this is equivalent to multiplication by 4. Right shifting an unsigned quantity always fits the vacated bits with zero. Right shifting a signed quantity will fill with bit signs (``arithmetic shift'') on some machines and with 0-bits (``logical shift'') on others.

The unary operator ~ yields the one’s complement of an integer; that is, it converts each 1-bit into a 0-bit and vice versa. For example

   x = x & ~077

sets the last six bits of x to zero. Note that x & ~077 is independent of word length, and is thus preferable to, for example, x & 0177700, which assumes that x is a 16-bit quantity. The portable form involves no extra cost, since ~077 is a constant expression that can be evaluated at compile time.
As an illustration of some of the bit operators, consider the function getbits(x,p,n) that returns the (right adjusted) n-bit field of x that begins at position p. We assume that bit position 0 is at the right end and that n and p are sensible positive values. For example, getbits(x,4,3) returns the three bits in positions 4, 3 and 2, right-adjusted.

/* getbits:  get n bits from position p */
unsigned getbits(unsigned x, int p, int n)
{
	return (x >> (p+1-n)) & ~(~0 < < n);
}

The expression x >> (p+1-n) moves the desired field to the right end of the word. ~0 is all 1-bits; shifting it left n positions with ~0< places zeros in the rightmost n bits; complementing that with ~ makes a mask with ones in the rightmost n bits.


Exercise 2-6. Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.

unsigned setbits(unsigned x, int p, int n, unsigned y)
{
	return (x & ((~0 < < (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0 << n)) << (p + 1 - n));
}

下面这个比较快:

unsigned setbits(unsigned x, int p, int n, unsigned y)
{
	return x & ~(~(~0 < < n)  << (p+1-n)) |
	      (y &   ~(~0 << n)) << (p+1-n));
}

阅读全文...

分类: C_CPP_CS 标签: ,

[ZT]关于vista的aero效果

2010年6月15日 Galaxy 没有评论

http://beatricesmth.blog41.fc2.com/blog-entry-25.html

我不要这种充满了IT文的宅用blog啊~~(碎碎念)

总之这是今晚EP的结果。

vista的aero效果是使用所谓的桌面窗口管理器,也就是Desktop Window Manager(DWM)实现的,放狗以这个关键词一搜头条就是MSDN所以不用费神给连接了。

在默认情形下,DWM用aero的半透明效果渲染程序窗口的非客户区(non-client,嘛反正windows下面搞过点界面的都能明白吧),然后它也提供了两个API:DwmExtendFrameIntoClientArea和EnableBlurBehind来允许把半透明效果应用到客户区域。就这么简单。

真的么…………抱歉,这是孔明的陷阱。

如果你只是简单地把API应用到原有的程序上面的话,结果只有“惨不忍睹”四字……
阅读全文…

分类: Programming 标签: , ,

[ZT]关于一些特殊浮点数

2010年6月13日 Galaxy 没有评论

http://live.aulddays.com/tech/10/double-float-ind-nan-inf/
打印浮点数输出 1.#IND 1.#INF nan inf 等解决
2010-03-11 11:24:34 旧日重来

进行浮点数编程时,如果没有注意,常常会出现输出类似 1.#IND, 1.#INF 或者 nan, inf 之类奇怪的输出。这通常隐含了浮点数操作的异常。
阅读全文…

分类: C_CPP_CS 标签: ,

[ZT]菊花文和边框文实现原理

2010年6月8日 Galaxy 没有评论

http://be-evil.org/post-179.html
这博客真的很坏,js代码是用图片给的,非逼我抄一份……

菊花文和边框文实现原理
作者:朦朧中的罪惡

最近网上开始流行神奇的菊花文和边框文,比原来的火星文更容易读取也更容易突破关键词防御系统

菊花文表现形式为:朦҉胧҉中҉的҉罪҉恶҉

边框文表现形式为:[̲̅朦̲̅胧̲̅中̲̅的̲̅罪̲̅恶̲̅]
阅读全文…

分类: HTML_CSS_JS 标签: ,

Google Pac-Man 首页简单分析

2010年5月22日 Galaxy 1 条评论

前天临晨就看到了,一猜就是js的。昨天忙,没分析,今天看到cB上有帖子说,就也分析了下。
阅读全文…

分类: HTML_CSS_JS 标签: ,

[Tip]awk笔记

2010年5月19日 Galaxy 没有评论

指定输出的域分隔符:

awk -vOFS="\t" '{print $1,$2,$3,$4,$6}'
awk 'BEGIN { OFS = "\t" } {print $1,$2,$3,$4,$6}'

阅读全文…

分类: Written 标签: , ,

[整理]用CSS自动缩小过宽图片

2010年2月21日 Galaxy 没有评论

简单说,如果要全局改,就是:

img {
	max-width: 600px;
	width: expression(this.width > 600 ? 600: true);
	height: auto;
}

对WP的主题,就是改style.css
阅读全文…

分类: HTML_CSS_JS 标签: , ,

[原创]用Perl写的归并排序(Merge sort)

2010年2月11日 Galaxy 没有评论

说是排序,其实大家用的时候往往都是来合并其他方法排序的局部结果的。
英文wiki直接写具体到排序方法最后才谈大家咋用,中文wiki就上来就谈合并,后面才讲咋排序。老外从小到大,咱从大到小,倒也是常态。
阅读全文…

Locations of visitors to this page