This is what I have to code.
Input an array of numbers(any numbers that user inputs from standard input, aka keyboard), remove numbers from array that are not 0 and 1. Thay array should now be bitwise moved with << or >> (again user choses the number of spaces moved and a type of move). After all of that turn that array into a binary number. Max number of elements is defined as a header with #define max_num_el 255.

I have issues with part of code that need me to move elements of array and turning an array into binary number.

Dani AI

Generated

Quick diagnosis and a compact plan (responding to , echoing 's request for code and 's temporary-variable hint): three separate issues cause the observed behavior — using the C bit-shift operators on integer elements instead of shifting array positions, unsafe/incorrect input handling, and attempting to pack an arbitrary-length bit array into a fixed-width integer (overflow risk for >64 bits). The correct choices are: represent bits as bytes (uint8_t), move the element positions with memmove/loops, and only use <</>> on integers when you have packed bits into an integer type that can hold them.

Safe element shifts (move positions, fill vacated slots with 0):

#include <string.h>

void shift_left(uint8_t *a, size_t n, size_t p) {
    if (p >= n) { memset(a, 0, n); return; }
    memmove(a, a + p, n - p);
    memset(a + n - p, 0, p);
}

void shift_right(uint8_t *a, size_t n, size_t p) {
    if (p >= n) { memset(a, 0, n); return; }
    memmove(a + p, a, n - p);
    memset(a, 0, p);
}

Converting the bit array to a numeric value:

  • If n <= 64, pack into unsigned long long safely:
    unsigned long long bits_to_ull(const uint8_t *a, size_t n) {
      unsigned long long v = 0;
      for (size_t i = 0; i < n; ++i) v = (v << 1) | (a[i] & 1);
      return v;
    }
  • If n > 64, produce a string of '0'/'1' or use a big-integer library (GMP) / a custom bitset; never assume unsigned long can hold 255 bits.

Practical fixes: use int main(void), check scanf return values and use scanf("%d", &x) (no trailing dot), prefer fgets+strtol for robust input, use uint8_t for per-bit storage, and test shifts with small arrays first to confirm behavior. These changes reconcile the observed multiplication-from-<< with the intended array-index shift and avoid overflow when converting to a binary number.

Recommended Answers

All 4 Replies

Post the code you have written so that we can see where your "issues" are.

use temporary variable for moving the items and for binary conversion ,you can easily write code ..

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define max_br_el 255

void main (){
    int p,k,j,n,i,temp,r;
    unsigned long a[max_br_el], b[max_br_el];
    while(1){
        printf("\nEnter number of array elements (between 1 i 255)\n");
        scanf("%d", &n);
        if(n>0 && n<=max_br_el){
            for(i=0 ; i<n ; i++){
                printf("Enter %d. elements of array\n", i);
                scanf("%d.", &a[i]);
            }
            /* Removing nums not 0 and 1 */
            for(i=0 , k=0  ; i<n ; i++){
                if(a[i] == 0 || a[i] == 1){
                    a[k] = a[i];
                    k++;
                }
            }
            n=k;
            for(k=0 ; k<n ; k++){
                printf("%d\t" , a[k]);
            }
            /* My try on moving elements manualy (it has to be done with                  << and >> but I don't know how. Whenever I try to use << and                  >> I get element_of_array*2^n (where and n is number of                      spaces that needs to be moved. */
            printf("\n");
            printf("\nEnter for how many spaces you want to move\n");
            scanf("%d", &p);
            for(r=0 ; r<n; r++){
                b[r] = a[k-p];

            }
            printf("\n");
            for(r=0 ; r<n ; r++){
                printf("%d" , b[r]);
            }
        }
        else{
            exit(1);
        }
    }
}

/* This is code so far. I still don't have part about binary conversion made, because I have bigger issues with << and >>. Here I tryed implementing manual moving of elements of array but it didn't work. Thanks for help. */

I'll show the code tonight, and making this question to solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.