src/HOL/Library/Bit_Operations.thy
changeset 71986 76193dd4aec8
parent 71965 d45f5d4c41bd
child 71991 8bff286878bf
equal deleted inserted replaced
71985:a1cf296a7786 71986:76193dd4aec8
   378         (simp_all add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff,
   378         (simp_all add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff,
   379         simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc)
   379         simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc)
   380   qed
   380   qed
   381 qed
   381 qed
   382 
   382 
       
   383 lemma take_bit_set_bit_eq:
       
   384   \<open>take_bit n (set_bit m w) = (if n \<le> m then take_bit n w else set_bit m (take_bit n w))\<close>
       
   385   by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_set_bit_iff)
       
   386 
       
   387 lemma take_bit_unset_bit_eq:
       
   388   \<open>take_bit n (unset_bit m w) = (if n \<le> m then take_bit n w else unset_bit m (take_bit n w))\<close>
       
   389   by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_unset_bit_iff)
       
   390 
       
   391 lemma take_bit_flip_bit_eq:
       
   392   \<open>take_bit n (flip_bit m w) = (if n \<le> m then take_bit n w else flip_bit m (take_bit n w))\<close>
       
   393   by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_flip_bit_iff)
       
   394 
   383 end
   395 end
   384 
   396 
   385 
   397 
   386 subsection \<open>Instance \<^typ>\<open>int\<close>\<close>
   398 subsection \<open>Instance \<^typ>\<open>int\<close>\<close>
   387 
   399 
   563   by (simp add: flip_bit_def)
   575   by (simp add: flip_bit_def)
   564 
   576 
   565 lemma flip_bit_negative_int_iff [simp]:
   577 lemma flip_bit_negative_int_iff [simp]:
   566   \<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
   578   \<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
   567   by (simp add: flip_bit_def)
   579   by (simp add: flip_bit_def)
       
   580 
       
   581 lemma and_less_eq:
       
   582   \<open>k AND l \<le> k\<close> if \<open>l < 0\<close> for k l :: int
       
   583 using that proof (induction k arbitrary: l rule: int_bit_induct)
       
   584   case zero
       
   585   then show ?case
       
   586     by simp
       
   587 next
       
   588   case minus
       
   589   then show ?case
       
   590     by simp
       
   591 next
       
   592   case (even k)
       
   593   from even.IH [of \<open>l div 2\<close>] even.hyps even.prems
       
   594   show ?case
       
   595     by (simp add: and_int_rec [of _ l])
       
   596 next
       
   597   case (odd k)
       
   598   from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems
       
   599   show ?case
       
   600     by (simp add: and_int_rec [of _ l])
       
   601 qed
       
   602 
       
   603 lemma or_greater_eq:
       
   604   \<open>k OR l \<ge> k\<close> if \<open>l \<ge> 0\<close> for k l :: int
       
   605 using that proof (induction k arbitrary: l rule: int_bit_induct)
       
   606   case zero
       
   607   then show ?case
       
   608     by simp
       
   609 next
       
   610   case minus
       
   611   then show ?case
       
   612     by simp
       
   613 next
       
   614   case (even k)
       
   615   from even.IH [of \<open>l div 2\<close>] even.hyps even.prems
       
   616   show ?case
       
   617     by (simp add: or_int_rec [of _ l])
       
   618 next
       
   619   case (odd k)
       
   620   from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems
       
   621   show ?case
       
   622     by (simp add: or_int_rec [of _ l])
       
   623 qed
       
   624 
       
   625 lemma set_bit_greater_eq:
       
   626   \<open>set_bit n k \<ge> k\<close> for k :: int
       
   627   by (simp add: set_bit_def or_greater_eq)
       
   628 
       
   629 lemma unset_bit_less_eq:
       
   630   \<open>unset_bit n k \<le> k\<close> for k :: int
       
   631   by (simp add: unset_bit_def and_less_eq)
   568 
   632 
   569 
   633 
   570 subsection \<open>Instance \<^typ>\<open>nat\<close>\<close>
   634 subsection \<open>Instance \<^typ>\<open>nat\<close>\<close>
   571 
   635 
   572 instantiation nat :: semiring_bit_operations
   636 instantiation nat :: semiring_bit_operations