#include <stdio.h>
#include <stdlib.h>
void main()
{
int testNums[] = {3, 0x12, 0xFF, -3};
int testBits[] = {9, 7, 12, 15};
int i;
for (i = 0; i < sizeof(testNums) / sizeof(testNums[0]); i++)
printBin(testNums[i], testBits[i]);
}
void printBin(int num, int bits)
{
int pow;
int mask = 1 << bits - 1;
for(pow=0; pow<bits; pow++)
{
if(mask & num)
printf("1");
else
printf("0");
num<<1;
}
printf("\n");
}
It doesn't print out the correct binary number, but has the right amount of bits, and advice on how I can fix this?
Your problem is here, in printBin:
num<<1;
This statement does nothing. What you want is:
num<<=1;
For clarity, you should also use parenthesis here:
int mask = 1 << (bits - 1);
And you should move printBin above main so the definition is visible at the time the function is called.
Finally, main should always return int.
There are several issues in your code:
You need a prototype for printBin().
Use int main(void) or int main(int argc, char** argv) instead of void main().
num<<1; is an expression that has no side effects. Perhaps you are able infer this from compiler's warning. Write num<<=1; to make a difference.
Only a suggestion: Why not replace the if statement with printf("%d", !!(mask & num));?
Here is the fixed code that compiles without warnings using clang:
#include <stdio.h>
#include <stdlib.h>
void printBin(int num, int bits);
int main(void)
{
int testNums[] = {3, 0x12, 0xFF, -3};
int testBits[] = {9, 7, 12, 15};
int i;
for (i = 0; i < sizeof testNums / sizeof testNums[0]; i++)
printBin(testNums[i], testBits[i]);
return 0;
}
void printBin(int num, int bits)
{
int pow;
int mask = 1 << (bits - 1);
for(pow=0; pow<bits; pow++)
{
printf("%d", mask & num);
num <<= 1;
}
putchar('\n');
}
Output:
000000011
0010010
000011111111
111111111111101
Although your approach is conventional and #dbush's answer is appropriate, I would like like to mention another approach. You may know this and it doesn't handle the negative values very well but in this approach, you can convert number of any base into other very easily
#include <stdio.h>
#include <string.h>
#define RESOLUTION 128
void compute(int number, int base, char* placeHolder){
int k = 0;
int i, j;
char temp;
while(number){
placeHolder[k++] = "0123456789ABCDEF"[number % base];
number /= base;
}
if (k != 0) {
placeHolder[k] = '\0';
for(i = 0, j = strlen(placeHolder) - 1; i < j; i++, j--){
temp = placeHolder[i];
placeHolder[i] = placeHolder[j];
placeHolder[j] = temp;
}
}else{
placeHolder[0] = '0';
placeHolder[1] = '\0';
}
}
int main(void) {
char bin[RESOLUTION];
compute(0xFF, 10, bin);
printf("%s\n", bin);
return 0;
}
Change the RESOLUTION value at you will.
More specific to binary, you can print binary of any datatype like this
void printBits(size_t const size, void const * const ptr)
{
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j;
for (i=size-1;i>=0;i--)
{
for (j=7;j>=0;j--)
{
byte = b[i] & (1<<j);
byte >>= j;
printf("%u", byte);
}
}
puts("");
}
int main()
{
int i = 23;
uint ui = UINT_MAX;
float f = 23.45f;
printBits(sizeof(i), &i);
printBits(sizeof(ui), &ui);
printBits(sizeof(f), &f);
return 0;
}
Related
I'm trying to write a program that converts from decimal to base 4 using bit masking. First I wrote this program that converts from decimal to binary to understand how it works
#include <stdio.h>
void binary(unsigned int num) {
int i;
unsigned temp, mask=1;
mask<<=31;
for(i=0; i<32; i++) {
temp=num&mask;
printf("%d", temp>>(31-i));
mask>>=1;
}
}
int main () {
int n;
printf("Enter a number:\n");
scanf("%d", &n);
binary(n);
return 0;
}
Then I was trying to use the same approach here
void base4(unsigned int num) {
int i;
unsigned temp, mask=3;
mask<<=15;
for(i=0; i<16; i++) {
temp= // <<<==== ???
printf("%d", temp>>(15-i)*2);
mask>>=1;
}
I know that in in base 4, numbers use 2 bits. So if we are ANDing each bit of a given value lets say 22 with the corresponding bit of the mask=3 we will end up with something like this
.... 0 0 1 1 2 (base4)
.... 00 00 01 01 10 (bin)
I just couldn't use this information to apply it to my previous approach, would be great if someone could help.
char *toBase4(char *buff, unsigned val, int printZeroes)
{
char *wrk = buff;
int shift = sizeof(val) * CHAR_BIT - 2;
unsigned mask = 3 << shift;
do
{
if((val & mask) || printZeroes)
{
*wrk++ = ((val & mask) >> shift) + '0';
printZeroes = 1;
}
mask >>= 2;
}while((shift -= 2) >= 0);
*wrk = 0;
return buff;
}
int main(void)
{
char x[256];
printf("%s\n", toBase4(x, 0xff00, 0));
printf("%s\n", toBase4(x, 0xff00, 1));
}
void base4(unsigned int num) {
int i;
unsigned int temp, mask=3;
mask<<=30;
for(i=0; i<16; i++) {
temp=num&mask;
printf("%d", temp>>(15-i)*2);
mask>>=2;
}
}
If I have an
int i = 11110001
How would I be able to convert this int into an int array where
int array[8] = {1, 1, 1, 1, 0, 0, 0, 1}
Using a little different approach and snprintf:
#include <stdio.h>
int main (void) {
int i = 11110001;
char arr[9]; //8 digits + \0
int array[8];
if ((snprintf(arr,9,"%d", i) == 8) { //return the 8 characters that were printed
int c;
for(c = 0; c < 8; c++)
array[c] = arr[c] - '0';
}
return 0;
}
P.S: I'm assuming positive values only
You may try like this:
#include <math.h>
char * convertNumber(unsigned int i) {
/* unsigned int length = (int)(log10((float)i)) + 1; */
/* char * arr = (char *) malloc(length * sizeof(char)), * x = arr; */
char * arr = malloc(8);
char * x = arr;
do
{
*x++ = i% 10;
i/= 10;
} while (i != 0);
return arr;
}
Try this :
#include<stdio.h>
void convert_int_to_array(unsigned int);
int main()
{
unsigned int a = 12345678;
convert_int_to_array(a);
return 0;
}
void convert_int_to_array(unsigned int a)
{
int array[25]; // array large enough for an integer
int i = 0, count = 0;
unsigned int num = a;
memset(array, '\0', 20); // I've not included the header file for this.
// gives a warning on compilation.
while(num > 0)
{
array[i] = num % 10;
num = num / 10;
++i;
++count;
}
for(i = count; i>=0;--i)
{
printf("array[%d] = %d\n",i, array[i]);
// or
printf("%d", array[i]);
// dont use both the printf statements, else you will see a
// messed up output.
}
}
BINARY REPRESENTATION :
#include<stdio.h>
struct bit
{
int a : 1;
};
int main()
{
struct bit b;
int d ,f,i;
d=f=256; // take any number of your choice
printf("binary representation of 256:\n");
for(i = 15; i>=0 ; i--) //assuming that the number wont have more than
// 15 DIGITS
{
f=f>>i;
b.a = f;
//d= d>>1;
f=d;
printf("%d",b.a);
}
return 0;
}
Is there any way to undo the actions or get the original array after I changed the array as shown below.
#include <stdio.h>
void function(int array[]){
array[2] = 20;
//do some extra work
return;
}
int main(void){
int array[5] = {1,2,3,4,5};
function(array);
// code which has to use original array
return 0;
}
You can pack two 32 bit integers (old / new) into a 64 bit integer, example:
#include <stdio.h>
#include <stdint.h>
void function(int64_t array[])
{
array[2] = (array[2] << 32) | 20;
}
void printarr(int64_t array[], size_t n)
{
size_t i;
for (i = 0; i < n; i++) {
printf("%d ", (int32_t)(array[i]));
}
printf("\n");
}
int main(void)
{
int64_t array[] = {1, 2, 3, 4, 5};
size_t i, n = sizeof(array) / sizeof(array[0]);
function(array);
puts("After function:");
printarr(array, n);
for (i = 0; i < n; i++) {
if (array[i] >> 32 != 0) /* Changed */
array[i] = array[i] >> 32; /* Undo */
}
puts("Original values:");
printarr(array, n);
return 0;
}
Output:
After function:
1 2 20 4 5
Original values:
1 2 3 4 5
Note:
Of course you can pack two 16 bit integers in a 32 bit integer if you are using short values in order to save some space.
To be portable use PRId32 format (defined in <inttyes.h>) for printf and int32_t:
printf("%"PRId32" ", (int32_t)x);
Another method:
If those changes are made sequentially over positive integers you can change the sign (to identify a change) and store only the changes using realloc:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *value;
size_t length;
} t_undo;
void function(t_undo *undo, int array[], int index, int value)
{
undo->value = realloc(undo->value, sizeof(int) * (undo->length + 1));
/* check realloc */
undo->value[undo->length++] = array[index];
array[index] = -value;
}
void printarr(int array[], size_t n)
{
size_t i;
for (i = 0; i < n; i++) {
printf("%d ", abs(array[i]));
}
printf("\n");
}
int main(void)
{
t_undo *undo;
int array[] = {1, 2, 3, 4, 5};
size_t i, j = 0, n = sizeof(array) / sizeof(array[0]);
undo = malloc(sizeof(*undo));
/* check malloc */
undo->value = NULL;
undo->length = 0;
function(undo, array, 2, 20);
puts("After function:");
printarr(array, n);
for (i = 0; i < n; i++) {
if (array[i] < 0) /* Changed */
array[i] = undo->value[j++]; /* Undo */
}
puts("Original values:");
printarr(array, n);
free(undo->value);
free(undo);
return 0;
}
Making it short: No. You cannot have your operations reverted - Not in a simple way anyway. What you probably want is to have a local copy of your data in your function(). You could do this by creating a new array and copy it to your first one:
int array[5] = whatever;
int array_copy[5];
memcpy(array_copy, array, 5*sizeof(int));
function(array_copy);
//You have your array now unchanged
If you really don't wanna do that, there's another way of enclosing your array into a struct and let the compiler synthesize a copy operation for you. However there's a serious downside for this one since for big such struct you may end up wasting your stack.
struct MyArray {
int array[5];
};
void function(struct MyArray m) {
//This will create a local copy of m
int i;
for (i = 0; i < 5; i++) {
m.array[i] = i + 1;
}
}
void initialize(struct MyArray* m) {
int i;
assert(m != NULL);
for (i = 0; i < 5; i++) {
m->array[i] = i;
}
}
int main(int argc, char *argv[]) {
struct MyArray m;
int i;
initialize(&m);
function(m);
//Your m here will be untouched
return 0;
}
#include <stdio.h>
void function(int array[]){
array[2] = 20;
}
void save(const char *filename, const void *data, size_t size);
void undo(const char *filename);
int main(void){
int array[5] = {1,2,3,4,5};
save("save_2", &array[2], sizeof(int));//array[2] save to file
function(array);
undo("save_2");//restore
save("save_all", array, sizeof(array));
function(array);
undo("save_all");
// code which has to use original array
for(int i=0;i<5;++i)
printf("%d\n", array[i]);
remove("save_2");
remove("save_all");
return 0;
}
void save(const char *filename, const void *data, size_t size){
FILE *fp = fopen(filename, "wb");
fwrite(&data, sizeof(void *), 1, fp);
fwrite(&size, sizeof(size_t), 1, fp);
fwrite(data, size, 1, fp);
fclose(fp);、
}
void undo(const char *filename){
FILE *fp = fopen(filename, "rb");
void *data;
size_t size;
fread(&data, sizeof(void*), 1, fp);
fread(&size, sizeof(size_t), 1, fp);
fread(data, size, 1, fp);
fclose(fp);
}
If one has a character array such as
char bytes[256] = "10000011011110110010001101000011";
I want to unsigned value of this which would be : 2205885251
I'm trying to do something along these lines
unsigned int arr[256];
for(int i = 0, k=0; i<256; i++, k++)
{
arr[k] = bytes[i]|bytes[i+1]<<8|bytes[i+2]<<16|bytes[i+3]<<24;
}
I am obtaining the wrong value: 3220856520, can anyone point out what I am doing wrong?
#include <stdio.h>
char bytes[256] = "10000011011110110010001101000011";
int main(void)
{
unsigned int out;
int i;
for (out = 0, i = 0; i < 32; ++i)
if (bytes[31 - i] == '1')
out |= (1u << i);
printf("%u\n", out);
return 0;
}
Output is:
2205885251
#include <stdio.h>
int main()
{
char bytes[256] = "10000011011110110010001101000011";
unsigned int value = 0;
for(int i = 0; i< 32; i++)
{
value = value *2 + (bytes[i]-'0');
}
printf("%u\n",value);
}
it outputs: 2205885251
char bytes[] = "10000011011110110010001101000011";
unsigned int k;
k = strtoul(bytes, NULL, 2);
printf("%u \n", k);
valter
I am trying to find primes using Sieve of Eratosthenes with bit arrays, but I am using unsigned int array. I need to be able to generate upto 2,147,483,647 primes. My code works and can generate around 10,000,000 but when I increase the size of my array to accommodate larger numbers it fails. Can someone guide me on how to use bit vectors with c (not c++).
Thanks
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#define MAXBYTES 2000000
#define MAX 50000000
#define BITSIZE 32
void ClearBit(unsigned int [], unsigned int);
void SetBit(unsigned int [], unsigned int);
int BitVal(unsigned int [], unsigned int);
void PrintBitStream(unsigned int [], unsigned long);
void PrintBitStreamData(unsigned int[], unsigned long);
int Sieve(unsigned int[], unsigned int, unsigned int);
int main(int argc, char ** argv) {
unsigned int maxsize = MAX;
unsigned int i;
//Set Bit Array
unsigned int BitArray[MAXBYTES] = {0};
SetBit(BitArray, 0);
SetBit(BitArray, 1);
i = 2;
for (;i < maxsize;i++){
if(Sieve(BitArray, i, maxsize)==0)
break;
}
PrintBitStreamData(BitArray, maxsize-1);
return EXIT_SUCCESS;
}
void PrintBitStreamData(unsigned int BitArray[], unsigned long maxsize) {
unsigned int i;
for (i = 0; i < maxsize; i++)
if (!BitVal(BitArray, i))
printf("%ld ", i);
printf("\n");
}
void PrintBitStream(unsigned int BitArray[], unsigned long maxsize) {
unsigned int i;
for (i = 2; i < maxsize; i+=2)
printf("%d", BitVal(BitArray, i));
printf("\n");
}
void SetBit(unsigned int BitArray[], unsigned int pos) {
BitArray[pos / BITSIZE] |= 1 << (pos % BITSIZE);
}
void ClearBit(unsigned int BitArray[], unsigned int pos) {
BitArray[pos / BITSIZE] &= ~(1 << (pos % BITSIZE));
}
int BitVal(unsigned int BitArray[], unsigned int pos) {
return ((BitArray[pos / BITSIZE] & (1 << (pos % BITSIZE))) != 0);
}
int Sieve(unsigned int BitArray[], unsigned int p, unsigned int maxsize) {
unsigned int i;
unsigned int j;
j = 0;
for (i = 2 * p; i < maxsize; i += p) {
SetBit(BitArray, i);
j++;
}
return j;
}
I definitely would NOT use a bit array, but an array of the native int (64-bit or 32-bit dependign on architecture) and wrapping around a function to remap normal numbers to the right place and bit with bitwise | and &.
Also consider leaving out the even numbers, almost none of them are prime. So you could store the first 128 numbers in the first 64-bit number, the next 128 in the second etc.
It sounds a bit complicated, but is a bit fun to get it work!
Project Euler seems to have produced some really nice solutions.
The good thing is: to sieve you don't need to recalculate the even-odd-transfer, but you can unset every third bit for sieving 3, every 5th bit for sieving 5 etc.
Come into chat if you want a quick java solution as a detailed reference.
EDIT4: corrected Working code, but yet slow. Memo: Remember using calloc!
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
typedef unsigned long long number;
number lookFor = 2147483648ULL;
number max = 2147483648ULL*10ULL; // hopefully more then every 10th uneven number is prime
unsigned long * isComposite;
number bitslong = CHAR_BIT*sizeof(long);
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
// is not called during sieve, only once per sieving prime
// and needed for reading if a number is prime
inline long getParts(number pos, number *slot, unsigned int *bit){
*slot = pos / bitslong;
*bit = (unsigned int)(pos % bitslong);
}
int isPrime(number n){
if(n == 1){
return 0;
}
if(n < 4){
return 1;
}
if((n%2) == 0){
return 0;
}
number slot=0;
unsigned int bit=0;
number pos = (number)(n-3)/2;
getParts(pos, &slot, &bit);
// printf("? n=%lld pos = %lld slot = %lld bit = %lu ret %d \n", n, pos, slot, bit, !(isComposite[slot] & (1<<bit)));
return !(isComposite[slot] & (1UL<<bit));
}
// start sieving at offset (internal position) offset with width step
int doSieve(number offset, number step){
rawtime = time(0);
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime(buffer, 80, "%Y-%m-%d %H:%I:%S", timeinfo);
unsigned int bit=0;
number slot=0;
getParts(offset, &slot, &bit);
printf("doSieve %s %lld %lld %lu \n", buffer, offset, step, isComposite[slot]);
while(slot < max/bitslong){
slot += (step + bit)/bitslong;
bit = (step + bit) % bitslong;
isComposite[slot] |= (1UL << bit);
}
return 1;
}
int sieve(){
number spot;
spot=1;
number pos;
pos = 0;
while(spot < 1 + sqrt((float)max)){
spot+=2;
if(! isPrime(spot)){
pos++;
continue;
}
doSieve(pos, spot);
pos++;
}
}
void main(int argc, char *argv[]){
if(argc > 1){
char *tp = malloc(sizeof(char*));
max = strtol(argv[1], &tp, 10);
}
printf("max %lld , sq %ld, malloc: %lld\n", max, (long)(1 + sqrt((float)max)), 1+max/bitslong);
isComposite = calloc((2+max/bitslong), sizeof(unsigned long)) ;
if(! isComposite){
printf("no mem\n");
exit(5);
}
sieve();
number i;
number found = 0;
for(i = 1; i<max && found < lookFor; i++){
if(isPrime(i)){
found++;
// printf(" %30lld %30lld \n", found, i);
if(found % 10000 == 0 ){
printf("%30lld %30lld \n", found, i);
}
}
/*
if(i % 1000 == 17){
printf("%5lld %5lld \n", i, found);
}
*/
}
}
Example using bit access into an integer
Note GetBit() and SetBit().
An optimizing compiler will make the / and % fast for powers of 2 are used.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ubitsize (sizeof(unsigned)*8)
unsigned GetBit(const unsigned *List, unsigned long index) {
return !!(List[index / ubitsize] & (1u << (index % ubitsize)));
}
void SetBit(unsigned *List, unsigned long index) {
List[index / ubitsize] |= (1u << (index % ubitsize));
}
void Sieve_of_Eratosthenes_via_bit_array(unsigned long MaxCandidatePrime) {
unsigned long uByteSize = MaxCandidatePrime/ubitsize + 1;
unsigned *List = calloc(uByteSize, sizeof *List);
if (List == 0) return;
unsigned long PrimeCount = 0;
unsigned long Index = 0;
for (Index = 2; Index <= MaxCandidatePrime; Index++) {
// If found a prime ...
if (GetBit(List, Index) == 0) {
PrimeCount++;
// let's see the progress
if ((PrimeCount % (256LU*1024)) == 0) printf("%lu\n", Index);
// Mark subsequent multiples as not--a-prime
unsigned long Index2 = Index*2;
while (Index2 <= MaxCandidatePrime) {
SetBit(List, Index2);
Index2 += Index;
}
}
}
printf("X %lu\n", Index);
free(List);
}
void test(void) {
Sieve_of_Eratosthenes_via_bit_array(200LU*1000*1000);
}
A re-write could employ usual suggestion to not save even numbers, treating 2 as a special case. This helps but I assume this is an exercise. I could save a factor of about 4 by saving using 1 byte to encode every 30th multiple as, after 30, for there are at most 8 primes every 30 integers. Other schemes exist.