Форум » C/C++ для начинающих (C/C++ for beginners) » Bit operations » Ответить

Bit operations

ascmax: 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; }

Ответов - 13

Сыроежка: 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.

ascmax: /* 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 ?

Сыроежка: That is the function.


ascmax: 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!

Сыроежка: You have to compile the program as a console application instead of a Windows application.

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

Сыроежка: I have no Code::Blocks. So I do not know how to set properties of a project. Try to find this yourself.

ascmax: 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)

Сыроежка: You have to enter some values for x, p and n and then output the result. For example [pre2] 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 } [/pre2]

ascmax: 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.

Сыроежка: Write [pre2] printf( "\n%d\n", getbits( x, p, n ) ); [/pre2]

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

Сыроежка: 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.



полная версия страницы