On-line: гостей 0. Всего: 0 [подробнее..]
Программисты всех стран, объединяйтесь!

АвторСообщение



ссылка на сообщение  Отправлено: 28.05.14 12:24. Заголовок: Bit operations


The task is follow: Implement function unsigned getbits( unsigned x, int p, int n ) which would return a bitfield containing bits of x starting from position p and of length n. It is assumed that bit in position 0 is the rightmost bit. Do not make any assumptions about the validity of p and n values.
I really dont get the idea of that task but I have some raw material which I have modified:

Can somebody give me tips or code how is it done ?

#include <stdio.h>

void showbits(unsigned int x)
{
int i;
int n=2; /*Length*/
for(i=(sizeof(int)*n)-1; i>=0; i--)
(x&(1<<i))?putchar('1'):putchar('0');

printf("\n");
}

int main()
{
int j = 14;
printf("The decimal %d is equal to binary - ", j);
showbits(j);
return 0;
}


Спасибо: 0 
ПрофильЦитата Ответить
Ответов - 13 [только новые]





ссылка на сообщение  Отправлено: 28.05.14 12:42. Заголовок: This assignment is t..


This assignment is taken from book "The C programming language" by Brian W.Kernigham and Dennis M. Ritchie. See the chapter whether bitwise operators are described.


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 28.05.14 13:15. Заголовок: /* getbits: get n bi..


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

So really, that´s the whole program ?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 28.05.14 13:26. Заголовок: That is the function..


That is the function.

Спасибо: 1 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 28.05.14 13:41. Заголовок: Doesn´t work on..


Doesn´t work on Code::Blocks ... the only include needed is stdio.h which I included , am I missing something ?
If someone is used to Code::Blocks then the error is " undefined reference to `WinMain@16' "

Or do I have to value the variables p and n ?
Sorry for the dumb questions!

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 28.05.14 14:01. Заголовок: You have to compile ..


You have to compile the program as a console application instead of a Windows application.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 28.05.14 14:11. Заголовок: Havent really done t..


Havent really done that before. Can you explain more ?
Google didn´t give any specific answer.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 28.05.14 14:22. Заголовок: I have no Code::Bloc..


I have no Code::Blocks. So I do not know how to set properties of a project. Try to find this yourself.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 29.05.14 16:30. Заголовок: The int main functio..


The int main function is missing, thats why the program would not start.

Doesnt it suppose to be like that:
#include <stdio.h>

unsigned getbits(unsigned x, int p, int n)
{
return (x >> (p+1-n)) & ~(~0 << n);
}
int main()
{
getbits(x);
printf("%d",x);
return 0;
}

I am struggling with declaring x ( dont know where to do that)

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 29.05.14 16:44. Заголовок: You shou;d eneter so..


You have to enter some values for x, p and n and then output the result. For example

 
int main( void )
{
int x;
int p;
int n;

// enter x, p, and n using scanf
// and output the return value of the function call
}



Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 29.05.14 16:55. Заголовок: Applying the getbits..


Applying the getbits function in the main() part is probably faulty.

int main()
{
int x, p, n;
printf("Insert bits\n");
scanf("%d",&x);
printf("Insert position\n");
scanf("%d",&p);
printf("Insert length\n");
scanf("%d",&n);
getbits(x,p,n);
printf("%d",x);
return 0;}

It returns me the same value as user enters.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 29.05.14 17:02. Заголовок: Write printf( "..


Write
 
printf( "\n%d\n", getbits( x, p, n ) );



Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 29.05.14 17:08. Заголовок: Hmmmm...every time t..


Hmmmm...every time the output is 0(zero)

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 29.05.14 17:34. Заголовок: Write the functiopn ..


Write the functiopn the following way

unsigned getbits(unsigned x, int p, int n)
{
return ( x >> p ) & ~( ~0 << n );
}

It is supposed that p starts from 0.


Спасибо: 0 
ПрофильЦитата Ответить
Ответ:
1 2 3 4 5 6 7 8 9
большой шрифт малый шрифт надстрочный подстрочный заголовок большой заголовок видео с youtube.com картинка из интернета картинка с компьютера ссылка файл с компьютера русская клавиатура транслитератор  цитата  кавычки моноширинный шрифт моноширинный шрифт горизонтальная линия отступ точка LI бегущая строка оффтопик свернутый текст

показывать это сообщение только модераторам
не делать ссылки активными
Имя, пароль:      зарегистрироваться    
Тему читают:
- участник сейчас на форуме
- участник вне форума
Все даты в формате GMT  3 час. Хитов сегодня: 19
Права: смайлы да, картинки да, шрифты да, голосования нет
аватары да, автозамена ссылок вкл, премодерация откл, правка нет