0

Im trying to make use of bitwise operators, and my program is crashing when it reaches this point. It looks something like this:

short x = 1;

short insert = x << (some_number - 1);
/*some_number is always > 0*/

array[y][z] &= insert;

Is this the best way to use these operators, and where might the bug be coming from?

array[y][z] is also a short, y and z are between 0 and 8 which was specified. some_number can not exceed 9 as to not go out of the bit range of a short int. The idea is to insert a bit into a certain place depending on the value of some_number.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 10
    The crash is not the bitwise operator's fault. The problem lies here `array[y][z]` . Checkt the values of `y` and `z`. And show how `array` is declared. – Jabberwocky Oct 08 '15 at 08:14
  • 3
    Start by using a debugger to locate the crash (the debugger will stop when the crash happens). Then go up the call stack to your code (if the debugger is not already there) and check the expression/statement where the crash happens, and especially check all variables involbed. Are there are `NULL` pointers? Any array indexes out of range? At the very least please update the question to include a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve), and mark out where the crash happens and what the values of all involved variables are. – Some programmer dude Oct 08 '15 at 08:15
  • [This](http://stackoverflow.com/questions/25989343/how-is-shift-operator-evaluated-in-c) might be of interest. – Lundin Oct 08 '15 at 08:16
  • 1
    Please show at least the declaration of `array`. – Jabberwocky Oct 08 '15 at 08:28
  • sorry im new to stack overflow. The array was declared in a [9][9] as a short int, and I am using a for statement for x and y: (x = 0; x < 9; x++) so it does not exceed 8. Also – Paul Wrobel Oct 08 '15 at 08:28
  • Without an [MCVE](http://stackoverflow.com/help/mcve) there is little we can do. You state "y and z are between 0 and 8 which was specified.". That doesn't mean that y a z are actually between 0 and 8. Did you check this ? You should definitly learn how to use a debugger. – Jabberwocky Oct 08 '15 at 08:29
  • Check that `some_number - 1` is between 0 and 14 (assuming you have 16-bit shorts) otherwise this shift has implementation-defined or undefined behaviour – M.M Oct 08 '15 at 08:37
  • 1
    You do not insert a bit into a certain place, you delete every bit except the one on the certain place. – mch Oct 08 '15 at 08:38
  • 1
    With the variables declared as you say, and with the values you state, there's no way the code could crash where you say it is, unless there's a bug in the compiler which generates fault code (highly unlikely but not impossible, do you use any special optimization flags?) You really need to run a debug-build in a debugger to locate the crash. – Some programmer dude Oct 08 '15 at 08:38
  • @mch even if `some_number` is big and the result of `<<` is undefined, I don't see how this could make the program crash. – Jabberwocky Oct 08 '15 at 08:42
  • @MichaelWalz i never said that this could crash the program. I only said that this code snippet does delete every bit but one, instead of setting the one. It was a comment that the program will not do what the OP said it should do, even without a crash. – mch Oct 08 '15 at 10:35
  • Interestingly `&=` on a short could be UB, when the result of `&` doesn't fit into `short`. Consider `short x=-1; x&=~0xffff;` – user3528438 Oct 08 '15 at 14:12

0 Answers0