Skip to content

Instantly share code, notes, and snippets.

@Mervetafrali
Created March 8, 2017 15:20
Show Gist options
  • Select an option

  • Save Mervetafrali/36b1a0463154dee78b1c61f9601f90db to your computer and use it in GitHub Desktop.

Select an option

Save Mervetafrali/36b1a0463154dee78b1c61f9601f90db to your computer and use it in GitHub Desktop.

Sistem Programlama 2.Hafta

1

Veri Tipleri (C Programlama Dili)

2

Saklayıcılar (Register)

3

C Dilindeki Bit operatörleri

4
5
6
7

AND, OR, XOR

#include<stdio.h>
int main()
{
	int a=12,b=10;
	printf("\nNumber1 AND Number2 : %d",a & b);
	printf("\nNumber  OR  Number2 : %d",a | b);
	printf("\nNumber  XOR Number2 : %d",a ^ b);
	return(0);
}
8
9
10

Sağa Bit Kaydırma (>>)

11
#include<stdio.h>
int main()
{
int a = 60;
printf("\nNumber is Shifted By 1 Bit  : %d",a >> 1);
printf("\nNumber is Shifted By 2 Bits : %d",a >> 2);
printf("\nNumber is Shifted By 3 Bits : %d",a >> 3);

return(0);
}
12

Sola Bit Kaydırma (<<)

13
#include<stdio.h>
int main()
{
int a = 60;
printf("\nNumber is Shifted By 1 Bit  : %d",a << 1);
printf("\nNumber is Shifted By 2 Bits : %d",a << 2);
printf("\nNumber is Shifted By 3 Bits : %d",a << 3);

return(0);
}
14

Bit Maskeleme

Gerekli verilerin tutulup ve kalanının maskelenmesi (bloke edildiği)
En Sık Kullanılan Operatör: AND (&)

15
16
17
18

Negatif Sayılar Bit Kaydırma

#include<stdio.h>
void main()
{
	printf("%x",-1<<4);
}

fffffff0

19
#include<stdio.h>
int main()
{
	int a = -60;

printf("\nNegative Right Shift by 1 Bit : %",a >> 1);
	printf("\nNegative Right Shift by 2 Bits : %d",a >> 2);
	printf("\nNegative Right Shift by 3 Bits : %d",a >> 3);


return(0);
}
20

Printf içinde Değişken Kaydırma

main()
{
	int i;
	for(i=0;i<5;i++)
   	printf("%d\n", 1L << i);
}
21

1’e Tümleme

22
#include<stdio.h>
int main()
{
	int a=10;
	printf("\nNegation of Number 10 : %d",~a);
	return(0);
}

Negation of Number 10 : -11

Örnek:

#define BIT_SET(VAR, BIT_NO) do { \
VAR |= 1<<BIT_NO; \
} while (0)


#define BIT_RESET(VAR, BIT_NO) do {    \
VAR &= ~(1<<BIT_NO); \
} while (0)



#define BIT_TOGGLE(VAR, BIT_NO) do { \
VAR ^= (1<<BIT_NO); \
} while (0)



#define BIT_GET(VAR, BIT_NO) ((VAR >> BIT_NO) & 1)


void print_bit(int var, int bit_count) {
    int i;
    for (i = bit_count-1 ; i >= 0 ; i--) {
        printf("%d", BIT_GET(var, i));
    }
    printf("\n");
}

int main() {

    int a = 0b00100;
    print_bit(a, 8);

    BIT_SET(a, 0); // a |= 0b1;
    print_bit(a, 8);

    BIT_RESET(a, 2); // a &= ~(0b100);
    print_bit(a, 8);

    BIT_TOGGLE(a, 3); // a ^= 0b1000;
    print_bit(a, 8);

    printf("a'nin 0. biti: %d\n", BIT_GET(a, 0));
    printf("a'nin 1. biti: %d\n", BIT_GET(a, 1));
    printf("a'nin 2. biti: %d\n", BIT_GET(a, 2));
    printf("a'nin 3. biti: %d\n", BIT_GET(a, 3));

    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment