Good software to code in C99? + some questions on bit masking

Write your question here.

What is a good software to code in c99? Something like eclipse will be awesome.
1)
1
2
3
4
5
/* Divides and takes the floor of a value exterior to this function by 2^POW.
* Does not use the division function. */
void div(int *y, unsigned int pow) {
*y = y[0] >> pow;
}


can someone explain what's the code doing above and what is ">>"

1
2
3
4
5
6
7
8
9
10
11
/* For each bit position i in [0, sizeof(int)*8) calls hello_world i times
* iff the ith bit of the value X points to is set. */
void HI_HI_HI_HI(int *x) {
int i = 0, j = 0, int_bits = sizeof(int) * 8;
for (i = 0; i < int_bits; i++) {
if ((x[0] >> i) & 1)
for (j = 0; j < i; j++)
hello_world();
}
}
1


to set a bit means transform something to 1, to reset a bit is to transform something to 0, to flip a bit is to switch 0 to 1 and 1 to 0. I would think the correct condition for the if loop is | 1 not & 1.

Set = f = f | 1
reset = f = f & 0
flip = f = f ^ 1
Last edited on
can someone explain what's the code doing above and what is ">>"

In this case the ">>" operator is being used as a "bitwise right shift" operator. Basically you take the number that you fed into 'y' in it's binary form; so for example the number '6' is represented by '110'. If you were to shift it right one time by passing the number '1' as the second argument you would get '011' which is '3' in decimal. Remember that the integers that are shifted out in this way are no longer part of the number, so shifting the original number '6' over two times leaves you with '001' which is the number '1' in decimal.

I would think the correct condition for the if loop is | 1 not & 1.
cringe please fix that. As for the rest of that sentence no. If you used the OR operator here it would always resolve to true since OR only requires either value to be set and the value your testing against is a constant '1'.
Last edited on
bump for software for c99
C99 compiler: either gcc or clang (with the -std=c99 -pedantic-errors options)

Avoid bitwise operations on signed integral types.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    int i = -1 ;
    std::cout << i << ' ' << unsigned(i) << '\n' ;
    i >>= 2 ;
    std::cout << i << ' ' << unsigned(i) << "\n\n" ;
    
    unsigned int u = -1 ;
    std::cout << signed(u) << ' ' << u << '\n' ;
    u >>= 2 ;
    std::cout << signed(u) << ' ' << u << '\n' ;
}

http://coliru.stacked-crooked.com/a/1ed441afcd0d84cb
bump for software for c99

If you are using Windows, I would suggest Pelles C IDE:
http://www.smorgasbordet.com/pellesc/
Any quick guide to pelles C IDE? I created a win32 empty project. I suspect I need to create a .c file and everything should be same as visual basics.
I suspect I need to create a .c file and everything should be same as visual basics.

New -> Source Code.
Write your code then when you save, it will add the file to the Project.

Edit: you should use the Wizard for Win32 projects though.

A Win32 program has a WinMain() function instead of main().
Last edited on
Topic archived. No new replies allowed.