C/Operatory: Różnice pomiędzy wersjami

Dodane 1501 bajtów ,  4 lata temu
m
Przy okazji warto zauważyć, że <code>a ^ b ^ b</code> to po prostu <code>a</code>. Właściwość ta została wykorzystana w różnych algorytmach szyfrowania oraz funkcjach haszujących. Alternatywę wyłączną stosuje się np. do szyfrowania kodu [[w:Wirus komputerowy|wirusów polimorficznych]].
 
 
=== Negacja bitowa ===
 
<source lang=c>
// gcc b.c -Wall
// ./a.out
#include <stdio.h>
 
/*
https://stackoverflow.com/questions/699968/display-the-binary-representation-of-a-number-in-c
Chris Lutz
*/
void PrintBitsOfUChar(unsigned char v) {
int i; // for C89 compatability
for(i = 7; i >= 0; i--) putchar('0' + ((v >> i) & 1));
}
 
 
 
void TestBitwiseNot (unsigned char a ){
 
printf ("decimal number a = %3u;\t ", a );
printf ("it's binary expansion = ");
PrintBitsOfUChar(a);
printf("\n");
printf ("decimal number ~a = %3u;\t ", (unsigned char) ~a );
printf ("it's binary expansion = ");
PrintBitsOfUChar((unsigned char) ~a);
printf("\n\n");
}
 
 
 
 
 
 
int main ()
{
//unsigned char a;
//char buffer[8];
printf("unsigned char has size = 1 byte = 8 bits and range from 0 to 255\n\n");
TestBitwiseNot(0);
TestBitwiseNot(255);
TestBitwiseNot(5);
TestBitwiseNot(3);
return 0;
}
</source>
 
 
Wynik
 
<source lang=bash>
unsigned char has size = 1 byte = 8 bits and range from 0 to 255
 
decimal number a = 0; it's binary expansion = 00000000
decimal number ~a = 255; it's binary expansion = 11111111
 
decimal number a = 255; it's binary expansion = 11111111
decimal number ~a = 0; it's binary expansion = 00000000
 
decimal number a = 5; it's binary expansion = 00000101
decimal number ~a = 250; it's binary expansion = 11111010
 
decimal number a = 3; it's binary expansion = 00000011
decimal number ~a = 252; it's binary expansion = 11111100
 
</source>
 
=== Przesunięcie bitowe ===
}
</source>
 
 
===Zastosowania===
7249

edycji