【问题标题】:Less then in bitwise operationLess then in bitwise operation
【发布时间】:2022-12-02 03:16:04
【问题描述】:

I have a task to practise and I just can't get the solution. It should be noted that I am a total beginner. There is 1min for the exercise, so it should be child's play. But I am racking my brains over it.

I am supposed to write an expression for the following :

Let x and k be variables of type int. Formulate a C condition with bitwise operator that is true exactly when x is less than 2^(k-1).

I would have done this with the ?-operator. So something like

( ) ? 1 : 0

But which condition must be in the brackets? I can't think of a bitwise operator that can be used to compare less than?

I really don't have any Idea :/

【问题讨论】:

    标签: c bit-manipulation less operator-keyword


    【解决方案1】:

    It seems you mean something like the following

    if ( x < 1 << k - 1 ) { /*...*/ }
    

    provided that the expression k - 1 is not negative.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-27
      • 2022-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 2021-04-28
      相关资源
      最近更新 更多