Check whether the given Integer
has an Alternate Pattern
#include
#include
void main()
{
int num, x, y, count = 0;
printf("enter the number:");
scanf("%d", &num);
x = num << 1;
y = x ^ num;
y = y + 1;
while
((y / 2) != 0)
{
if (y % 2 != 0)
{
count++;
break;
}
else
{
y = y / 2;
}
}
if (count)
{
printf("false");
}
else
{
printf("true");
}
getch();
}
Output
Enter the number:85
True
Enter the number:87
False
Round Floor of integer to next Lower Power of 2
#include
#include
#define num_bits_int 32
int count = 0;
void main()
{
int temp, n, bit, i = 0;
clrscr();
printf("Enter a number : ");
scanf("%d", &n);
temp = n;
while (i < num_bits_int)
{
bit = temp & 0x80000000;
if (bit == -0x80000000)
{
bit = 1;
}
printf("%d", bit);
temp = temp << 1;
i++;
}
getch();
}
Output
Enter a number : 128
00000000000000000000000010000000
Enter a number : 7
00000000000000000000000000000111
Enter a number : -127
11111111111111111111111110000001
Function to return MSB position of unsigned Integer
#include
#include
#define num_bits_int 32
int int_msb_position(int n);
void main()
{
int n, pos;
printf("Enter a number : ");
scanf("%d", &n);
pos = int_msb_position(n);
printf("\nPosition of MSB bit = %d\n", num_bits_int - (pos + 1));
}
int int_msb_position(int n)
{
int i = 0, bit;
while (i < num_bits_int)
{
bit = n & 0x80000000;
if (bit == -0x80000000)
{
bit = 1;
}
if (bit == 1)
break;
n = n << 1;
i++;
}
return i;
getch();
}
Output
Enter a number : 127
Position of MSB bit = 6
Enter a number : 259
Position of MSB bit = 8
Enter a number : 5
Position of MSB bit = 2
Bitwise Operations to Round(floor of) an Integer to next Lower Multiple of 2
#include
#include
void main()
{
int x = 1, i, n;
clrscr();
printf("enter the number :");
scanf("%d", &n);
if (n > 0)
{
for (; x <= n >> 1;)
{
x = x << 1;
}
n = x;
}
else
{
n = ~n;
n = n + 1;
for (; x <= n >> 1;)
{
x = x << 1;
}
x = x << 1;
x = ~x;
x = x + 1;
n = x;
}
printf("%d", n);
getch();
}
Output
Enter the number :9
8
Enter the number :44
32
Enter the number :-20
-32
Enter the number :-84
-128
Print the Range of Fundamental Data Types
#include
#include
#define size(x) sizeof(x)*8
void signed_one(int);
void unsigned_one(int);
void main()
{
printf("\nrange of int");
signed_one(size(int));
printf("\nrange of unsigned int");
unsigned_one(size(unsigned int));
printf("\nrange of char");
signed_one(size(char));
printf("\nrange of unsigned char");
unsigned_one(size(unsigned char));
printf("\nrange of short");
signed_one(size(short));
printf("\nrange of unsigned short");
unsigned_one(size(unsigned short));
}
void signed_one(int count)
{
int min, max, pro;
pro = 1;
while (count != 1)
{
pro = pro << 1;
count--;
}
min = ~pro;
min = min + 1;
max = pro - 1;
printf("\n%d to %d", min, max);
}
void unsigned_one(int count)
{
unsigned int min, max, pro = 1;
while (count != 0)
{
pro = pro << 1;
count--;
}
min = 0;
max = pro - 1;
printf("\n%u to %u", min, max);
getch();
}
Output
Range of int
-2147483648 to 2147483647
Range of unsigned int
0 to 4294967295
Range of char
-128 to 127
Range of unsigned char
0 to 255
Range of short
-32768 to 32767
Range of unsigned short
0 to 65535
No comments:
Post a Comment