+ 1

Some intersting BUG (C code)

Hello everyone, I planned to learn more about bitwise operations in C. I've started to write some code for playing with bits and got some bug. I don't understand where is it. So it's only the first part of the code, where User input a integer (I will be using it as a bit field for operations with bits), and another integer ( it will be the number of a bit in 32 bits integer that I will be use as a bitmask) and the last integer - is flag for bitmask (ON or OFF). So the problem: When user entered the third integer (var flagBit) the previous integer (var numBit) is equal zero. For example. I enter 10 for first integer (var number), 2 for second (var numBit), and 1 for third (var flagBit). At the and of the code the values of the vars are: var number ==10 var numBit == 0 var flagBit == 1. And I really don't understand why!!! Here is my code: https://code.sololearn.com/cmCQtALrAwr0

17th May 2020, 7:52 AM
Денис Филь
Денис Филь - avatar
2 odpowiedzi
+ 1
Didn't test your code, but... %u for unsigned integer not %d %d will treat the value as if it is an integer and if the number input is between the upper limit of int and the upper limit of unsigned int, will be negative. Shouldn't really be an issue, as it will just convert back to unsigned and they should have the same byte width. Also, curious as to why you're using goto?
17th May 2020, 9:18 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
You are right!!! Thanx a lot!
17th May 2020, 9:33 AM
Денис Филь
Денис Филь - avatar