CèšèªãŸãšã
C language, c.
æŒç®å
bit æŒç®
代衚çãªæŒç®å.
- AND: A&B
- OR: A|B
- XOR: A^B <=> (A&~B)|(~A&B)
- NOT: ~A
- DeMorganâs Low: ~(A|B) = ~A&~B
Twoâs Complement Arithmetic
2 ã®è£æ°. 2 ã®è£æ° - Wikipedia Integer ã® negative ãåŸãããã«ã¯, 以äžã®é¢ä¿ããªããã€.
~x + 1 = -x
x + 1 = ~x
ãããããããããªå®çãå°ãã.
/* ãã€ãã¹ 1 (2) */
+ ~0
(!A) = (A ^ 1)
(A ^ 1) ^ 1= (!A) ^ 1 = A
Bit æŒç® Tips
- ããããã®éèšåž³ ãããæŒç®åºæ¬æé
- http://www.hackersdelight.org/basics2.pdf
- Bit Twiddling Hacks
/* N ãããç®ãåãåºã */
(n >> N) & 1
/* N ãããç®ã« 1 ãã»ãããã */
n = n | (1 << N);
/* N ãããç®ã« 0 ãã»ãããã */
n = n & ~(1 << N);
/* N ãããç®ãå転 */
n = n ^ (1 << N);
/* 0 ãããç®ãã N-1 ãããç®ãŸã§ããã¹ãŠ 1 ã§ããåã®çæ */
n = (1 << N) - 1;
/* å
šãããã®å転 (1) */
n = ~n
/* å
šãããã®å転 (2) */
n = n ^ ~0
/* unsigned int ã®æäžäœãããã®å€ (1) */
1 << ( sizeof (unsigned) * 8 - 1 )
/* unsigned int ã®æäžäœãããã®å€ (2) */
~(~0U >> 1)
/* 1 ã§ããæãäžäœã®ãããã®å€ãæœåº */
-n & n
/* å¥æ°ãã©ããã®å€å® */
if ( n & 1 ) { /* å¥æ° */ }
絶察å€ãæ±ãã.
/* çµ¶å¯Ÿå€ */
int r = (v + mask) ^ mask;
int r = (v ^ mask) - mask;
inline int fastabs (int a)
{
int mask = a >> 31;
return (a ^ mask) - mask;
}
/* 2 æŽæ°ã® max, min */
inline int fastmax (int a, int b)
{
int t = (a-b);
return a - (t & (t >> 31));
}
inline int fastmin (int a, int b)
{
int t = (a-b);
return b + (t & (t >> 31));
}
if æã眮ãæã.
/* x ã 0 or 1 ãšä»®å®ãããš */
if (x) a=y else a=z;
a = x ? y : z;
a = ((x<<31) >> 31)& y + ((!x) << 31) >> 31) & z
ãã©ã°å¶åŸ¡ Tips
ã€ãã³ããã©ã°ã®å¶åŸ¡ãšããŠ, C èšèªã§ã¯ãããæŒç®ã倧掻èº!
/* ãã©ã°ã®ååŸ get */
x & y
/* ãã©ã°ãç«ãŠã set */
x | y
/* ãã©ã°ãè§£é€ clear */
x & ~y
x &= 0x10;
x |= 0x10;
x &= ~0x10;
ã·ããæŒç®
ä¹ç®ã»é€ç®
/* 2 ã® i ä¹åãã */
n <<= i;
y = x << 1; // y = x * 2;
/* 2 ã® i ä¹ã§å²ã */
n >>= i;
y = x >> 1; // y = x / 2;
/* 2 ã® i ä¹ã§å²ã£ãå°äœ */
n & ((1 << i) - 1)
ç®è¡ã·ãããšè«çã·ãã
è«çã·ããã¯ç¬Šå·ããã (å é ããã, 第 15 ããã, æãå·Šã®ããã) ãå«ããŠã·ãããããã®ã§, 空ãããããäœçœ®ãã¹ãŠã« 0 ãå ¥ããŸã.
ç®è¡ã·ããã¯ç¬Šå·ããããé€ããŠã·ãããããã®ã§, 空ãããããäœçœ®ãã¹ãŠã«å·Šã·ããã®æ㯠0, å³ã·ããã®æã¯ç¬Šå·ããããšåããã®ãå ¥ããŸã.
int logicalShift (int x, int n) {
int mask = ((1 << 31) >> n) << 1;
return (x >> n) & ~mask;
}
è«çæŒç®
è«çæŒç®å Tips
if ãè«çæŒç®ã§çœ®ãæãã.
/* if (A) B */
A && B;
/* if (!A) B */
A || B;
ç®è¡æŒç®
æ§é äœ
æ§é äœã®ãµã€ãºã¯, ã³ã³ãã€ã«ã決ãã. double, int 㯠char ãããå ã«å®£èšããã»ãã, ã¡ã¢ãªå¹çããã.
ðãã€ã³ã¿(C)
ã¡ã¢ãªç®¡ç
ðã¡ã¢ãªãªãŒã¯
ã¡ã¢ãªè§£æŸæŒã.
ðã¡ã¢ãªç Žå£
- å°çã®èŠãã¿ãã¡ã¢ãªç Žå£ãCppcheckã§è¯éºã«é£ãæ¢ãããïŒ | Futurismo
- ðã¡ã¢ãªãªãŒã¯/ã¡ã¢ãªç Žå£ã§å®æé瀟ã§ããã«æ·±å€æ®æ¥çªå ¥ããŸããã®æããæãåºããŠRustã®æææš©ãåŠã¶(23/09/04)
allocator
Explicit allocator
C ã® malloc ã free ã§åçã«ååŸã»éæŸããã.
Inplicit allocator
ã¢ããªã±ãŒã·ã§ã³ã«ãã£ãŠç²åŸãããã, éæŸãããªã.
Java ã®ã¬ããŒãžã»ã³ã¬ã¯ã·ã§ã³.
Topics
ðšããã¹ã»ãªãããŒ
- ãã«ç 究æ
- Cèšèªã®ãã.
ðããã°ã©ãã³ã°èšèªC - ãã©ã€ã¢ã³ã»ã«ãŒããã³/ããã¹ã»ãªãããŒ
K&Rãšç¥ã.
æŽå²çããã°ã©ãã³ã°èšèªã®åè. ðHello worldã¯ããããå§ãŸã£ã.
ããããæã£ãŠããã©å£²åŽãã.
Related
- tags: ðProgramming Language
- refs:
- posts
ðŠCèšèªwith me
ãããã®ã¯ãããŠåŠãã ããã°ã©ãã³ã°èšèª
ðµããã°ã©ãã³ã°ã®å匷ã§é«2ã®å€äŒã¿ã«CèšèªãåŠãã (2003/08)